Created
September 17, 2017 11:09
-
-
Save carlesba/5058f38ca153de205c6bccff14999bf4 to your computer and use it in GitHub Desktop.
Paths for functional pipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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