Skip to content

Instantly share code, notes, and snippets.

@carlesba
Created September 17, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlesba/5058f38ca153de205c6bccff14999bf4 to your computer and use it in GitHub Desktop.
Save carlesba/5058f38ca153de205c6bccff14999bf4 to your computer and use it in GitHub Desktop.
Paths for functional pipes
import {
curryN,
update,
nth
} from 'ramda'
// Getting an array, modify just one its elements allowing different pipes in a data structure
// Number -> Function -> Array -> Array
const path = curryN(
3,
(index, functor, datum) => update(
index,
functor(nth(index, datum))
datum
)
)
/*
Example:
const add1 = add(1)
const add9 = add(9)
// numberOfElements -> valueToFullFill -> List
// ex: fill(2, 3) === [3, 3]
const fill = flip(repeat)
const usePath = curryN(
3,
(index, functor, datum) => update(
index,
functor(nth(index, datum)),
datum
)
)
const do_x_plus_9_divided_by_x_plus_1 = compose(
apply(divide),
usePath(0, add9),
usePath(1, add1),
fill(2)
)
do_x_plus_9_divided_by_x_plus_1(1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment