Skip to content

Commit 2b2d079

Browse files
jethrolarsonhemanth
authored andcommitted
Updated Applicative and Lift to partially address recent feedback (#74)
1 parent 102809f commit 2b2d079

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

readme.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,29 @@ Array.prototype.of = (v) => [v];
315315

316316
## Lift
317317

318-
Lift is like `map` except it can be applied to multiple functors.
318+
Lifting is when you take a value and put it into an object like a [functor](#pointed-functor). If you lift a function into an [Applicative Functor](#applicative-functor) then you can make it work on values that are also in that functor.
319319

320-
Map is the same as a lift over a one-argument function:
320+
Some implementations have a function called `lift`, or `liftA2` to make it easier to run functions on functors.
321321

322322
```js
323-
lift((n) => n * 2)([2, 3, 4]); // [4, 6, 8]
323+
const mult = (a, b) => a * b;
324+
325+
const liftedMult = lift(mult); // this function now works on functors like array
326+
327+
liftedMult([1, 2], [3]); // [3, 6]
328+
lift((a, b) => a + b)([1, 2], [3, 4]); // [4, 5, 5, 6]
324329
```
325330

326-
Unlike map lift can be used to combine values from multiple arrays:
331+
Lifting a one-argument function and applying it does the same thing as `map`.
327332

328333
```js
329-
lift((a, b) => a * b)([1, 2], [3]); // [3, 6]
330-
lift((a, b) => a * b)([1, 2], [3, 4]); // [3, 6, 4, 8]
334+
const increment = (x) => x + 1;
335+
336+
lift(increment)([2]); // [3]
337+
[2].map(increment); // [3]
331338
```
332339

340+
333341
## Referential Transparency
334342

335343
An expression that can be replaced with its value without changing the
@@ -456,6 +464,24 @@ An applicative functor is an object with an `ap` function. `ap` applies a functi
456464
[(a) => a + 1].ap([1]) // [2]
457465
```
458466

467+
This is useful if you have multiple applicative functors and you want to apply a function that takes multiple arguments to them.
468+
469+
```js
470+
const arg1 = [1, 2];
471+
const arg2 = [3, 4];
472+
473+
// function needs to be curried for this to work
474+
const add = (x) => (y) => x + y;
475+
476+
const partiallyAppliedAdds = [add].ap(arg1); // [(y) => 1 + y, (y) => 2 + y]
477+
```
478+
479+
This gives you an array of functions that you can call `ap` on to get the result:
480+
481+
```js
482+
partiallyAppliedAdds.ap(arg2); // [3, 4, 5, 6]
483+
```
484+
459485
## Morphism
460486

461487
A transformation function.

0 commit comments

Comments
 (0)