ramda.js - Ramda chain usage -
from documentation:
var duplicate = n => [n, n]; r.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3] r.chain(r.append, r.head)([1, 2, 3]); //=> [1, 2, 3, 1] the first example straight forward, applies duplicate() every element in array , concatenates result. having trouble understanding second example. how mapping r.append + r.head on array? can please provide step step explanation second example ?
i familiar compose , currying.
thanks
the second example showing how r.chain can used things other arrays, such functions (or implementing fantasy land chain spec).
if concept of mapping , concatenating array familiar you, can think of mapping function on function plain function composition. concatenating part require further explanation.
r.chain declares signature as:
chain m => (a → m b) → m → m b for arrays, can swap m [] get:
(a → [b]) → [a] → [b] for functions receive argument r, becomes:
(a → r → b) → (r → a) → (r → b) so knowledge of types available, way produce final r → b function following:
- pass received argument
rsecond function producea - apply both new
a, originalrfirst function produce resultingb
or in code:
// specialised functions const chain = (firstfn, secondfn) => x => firstfn(secondfn(x), x) swapping in functions example, can see becomes:
x => r.append(r.head(x), x) if familiar r.converge effectively:
r.converge(firstfn, [secondfn, r.identity])
Comments
Post a Comment