Skip to content

Commit 979dbcb

Browse files
Fix minor issue and add utilities
1 parent 0523bd9 commit 979dbcb

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

library/Either.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Either.prototype.alt = Either.prototype["fantasy-land/alt"] = function (containe
5252
Either.prototype.ap = Either.prototype["fantasy-land/ap"] = function (container) {
5353

5454
return this.fold({
55-
Left: _ => container,
55+
Left: _ => this,
5656
Right: value => Either.Right.is(container) ? Either.Right(container[$$value](value)) : container
5757
});
5858
};

library/Pair.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { curry } from "https://x.nest.land/[email protected]/source/index.js";
2+
13
import { factorizeType } from "./factories.js";
24

35
/**
@@ -34,4 +36,6 @@ Pair.prototype.map = Pair.prototype["fantasy-land/map"] = function (unaryFunctio
3436
return Pair(unaryFunction(this.first), this.second);
3537
};
3638

39+
export const factorizePair = curry(Pair);
40+
3741
export default Pair;

library/Task.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Pair from "./Pair.js";
44
import { Done, Loop } from "./Step.js";
55

66
import { $$debug, $$inspect } from "./Symbols.js";
7+
import { chainLift } from "./utilities.js";
78

89
const concat = x => y => x.concat(y);
910

@@ -173,12 +174,6 @@ Task.prototype.chain = Task.prototype["fantasy-land/chain"] = function (unaryFun
173174
);
174175
};
175176

176-
// chainLift :: Task a -> Task b -> (a -> b -> c) -> Task c
177-
const chainLift = (chainableFunctor, functor, binaryFunction) => {
178-
179-
return chainableFunctor.chain(x => functor.map(binaryFunction(x)));
180-
}
181-
182177
Task.prototype.chainRec = Task.prototype["fantasy-land/chainRec"] = function (ternaryFunction, initialCursor) {
183178
let accumulator = this;
184179
let result = Loop(Pair(initialCursor, null));
@@ -187,7 +182,7 @@ Task.prototype.chainRec = Task.prototype["fantasy-land/chainRec"] = function (te
187182
result = ternaryFunction(Loop, Done, result.value.first);
188183

189184
if (Loop.is(result)) {
190-
accumulator = chainLift(accumulator, result.value.second, concat);
185+
accumulator = chainLift(concat, accumulator, result.value.second);
191186
}
192187
}
193188

library/utilities.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export const assertIsUndefined = value => value === undefined;
1818
export const decodeRaw = $$decoder.decode.bind($$decoder);
1919
export const encodeText = $$encoder.encode.bind($$encoder);
2020

21+
// chainLift :: (a -> b -> c) -> Task a -> Task b -> Task c
22+
export const chainLift = curry(
23+
(binaryFunction, chainableFunctor, functor) => chainableFunctor.chain(x => functor.map(binaryFunction(x)))
24+
);
25+
26+
// chainRec :: (a -> c, b -> c, a) -> a -> Task b -> Task c
27+
export const chainRec = curry(
28+
(ternaryFunction, initiator, task) => task.chainRec(ternaryFunction, initiator)
29+
);
30+
2131
// insideOut :: Applicative a => a -> a[] -> a
2232
export const insideOut = curry(
2333
(T, list) => list.reduce(

0 commit comments

Comments
 (0)