mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-10 20:46:51 +00:00
Address @i-am-tom and @shineli1984's review
This commit is contained in:
parent
5071b5a617
commit
c39c733ff6
1 changed files with 11 additions and 6 deletions
17
readme.md
17
readme.md
|
|
@ -698,7 +698,7 @@ pairToCoords(coordsToPair({x: 1, y: 2})) // {x: 1, y: 2}
|
||||||
|
|
||||||
### Catamorphism
|
### Catamorphism
|
||||||
|
|
||||||
A `reduceRight` function. Take a bunch of things, and combine them into another. The morphism is from "bunch of things" to "another".
|
A `reduceRight` function that applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = xs => xs.reduceRight((acc, x) => acc + x, 0)
|
const sum = xs => xs.reduceRight((acc, x) => acc + x, 0)
|
||||||
|
|
@ -734,17 +734,22 @@ The combination of anamorphism and catamorphism.
|
||||||
|
|
||||||
### Paramorphism
|
### Paramorphism
|
||||||
|
|
||||||
Enhancement of catamorphism. It's like `reduceRight`. However, there's a difference:
|
A function just like `reduceRight`. However, there's a difference:
|
||||||
|
|
||||||
In paramorphism, your reducer's arguments are the current value, the reduction of all previous values, and the list of values that formed that reduction.
|
In paramorphism, your reducer's arguments are the current value, the reduction of all previous values, and the list of values that formed that reduction.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Obviously not safe for lists containing `undefined`,
|
// Obviously not safe for lists containing `undefined`,
|
||||||
// but good enough to make the point.
|
// but good enough to make the point.
|
||||||
const para = (reducer, accumulator, [x, ... xs]) =>
|
const para = (reducer, accumulator, elements) => {
|
||||||
x !== undefined
|
if (elements.length === 0)
|
||||||
? reducer(x, xs, para(reducer, accumulator, xs))
|
return accumulator
|
||||||
: accumulator
|
|
||||||
|
const head = elements[0]
|
||||||
|
const tail = elements.slice(1)
|
||||||
|
|
||||||
|
return reducer(head, tail, para(reducer, accumulator, tail))
|
||||||
|
}
|
||||||
|
|
||||||
const suffixes = list => para(
|
const suffixes = list => para(
|
||||||
(x, xs, suffxs) => [xs, ... suffxs],
|
(x, xs, suffxs) => [xs, ... suffxs],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue