Skip to content

Commit c39c733

Browse files
committed
Address @i-am-tom and @shineli1984's review
1 parent 5071b5a commit c39c733

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

readme.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ pairToCoords(coordsToPair({x: 1, y: 2})) // {x: 1, y: 2}
698698

699699
### Catamorphism
700700

701-
A `reduceRight` function. Take a bunch of things, and combine them into another. The morphism is from "bunch of things" to "another".
701+
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.
702702

703703
```js
704704
const sum = xs => xs.reduceRight((acc, x) => acc + x, 0)
@@ -734,17 +734,22 @@ The combination of anamorphism and catamorphism.
734734

735735
### Paramorphism
736736

737-
Enhancement of catamorphism. It's like `reduceRight`. However, there's a difference:
737+
A function just like `reduceRight`. However, there's a difference:
738738

739739
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.
740740

741741
```js
742742
// Obviously not safe for lists containing `undefined`,
743743
// but good enough to make the point.
744-
const para = (reducer, accumulator, [x, ... xs]) =>
745-
x !== undefined
746-
? reducer(x, xs, para(reducer, accumulator, xs))
747-
: accumulator
744+
const para = (reducer, accumulator, elements) => {
745+
if (elements.length === 0)
746+
return accumulator
747+
748+
const head = elements[0]
749+
const tail = elements.slice(1)
750+
751+
return reducer(head, tail, para(reducer, accumulator, tail))
752+
}
748753

749754
const suffixes = list => para(
750755
(x, xs, suffxs) => [xs, ... suffxs],

0 commit comments

Comments
 (0)