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 r second function produce a
  • apply both new a , original r first function produce resulting b

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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -