Last active
August 5, 2017 20:31
-
-
Save gdekefir/538ecc1640a497d3bb041d9e1ea46b54 to your computer and use it in GitHub Desktop.
small ramda helpers
This file contains 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
const argsToArray = unapply(identity); | |
argsToArray(1, 3, 5); //-> [1, 3, 5] | |
//////////////////////////////////////////////////////////////// | |
const dropArgs = curryN(2, compose( | |
apply(drop), | |
splitAt(1), | |
argsToArray | |
)); | |
dropArgs(2)('a', 'b', 'c', 'd'); //-> ["c", "d"] | |
//////////////////////////////////////////////////////////////// | |
const dropArgsLast = curryN(2, compose( | |
apply(dropLast), | |
splitAt(1), | |
argsToArray | |
)); | |
dropArgsLast(2)('a', 'b', 'c', 'd'); //-> ["a", "b"] | |
//////////////////////////////////////////////////////////////// | |
const takeArgs = curryN(2, compose( | |
apply(take), | |
splitAt(1), | |
argsToArray | |
)); | |
takeArgs(2)('a', 'b', 'c', 'd'); //-> ["a", "b"] | |
//////////////////////////////////////////////////////////////// | |
const splice = converge(insertAll, [ | |
identity, | |
nthArg(3), | |
remove | |
]); | |
splice(1, 2, ['a', 'b', 'c', 'd', 'e'], ['add1', 'add2', 'add3']); //-> ["a", "add1", "add2", "add3", "d", "e"] | |
//////////////////////////////////////////////////////////////// | |
const sliceByCount = converge(slice, [identity, add, nthArg(2)]); | |
sliceByCount(1, 3, ['a', 'b', 'c', 'd', 'e']); //-> ["b", "c", "d"] | |
//////////////////////////////////////////////////////////////// | |
const reverseElems = converge(insertAll, [ | |
identity, | |
compose(reverse, sliceByCount), | |
remove | |
]); | |
reverseElems(1, 3, ['a', 'b', 'c', 'd', 'e']) //-> ["a", "d", "c", "b", "e"] | |
//////////////////////////////////////////////////////////////// | |
const reverseArgs = converge(compose, [ | |
compose(apply, nthArg(2)), | |
compose(apply(reverseElems), takeArgs(2)), | |
always(argsToArray) | |
]) | |
reverseArgs(1, 3, argsToArray)('a', 'b', 'c', 'd', 'e') //-> ["a", "d", "c", "b", "e"] | |
//////////////////////////////////////////////////////////////// | |
const repeatEl = converge( | |
converge(splice, [ | |
identity, | |
always(1), | |
nthArg(2), | |
converge(repeat, [nthArg(1), nthArg(3)]) | |
]), [ | |
identity, | |
converge(prop, [identity, nthArg(2)]), | |
nthArg(2), | |
nthArg(1) | |
] | |
) | |
repeatEl(1, 3, [5, 7, 12]); //-> [5, 7, 7, 7, 12] | |
//////////////////////////////////////////////////////////////// | |
const repeatArg = converge( | |
compose, [ | |
compose(apply, nthArg(2)), | |
compose(apply(repeatEl), takeArgs(2)), | |
always(argsToArray) | |
]); | |
const fn = argsToArray; | |
repeatArg(1, 3, fn)('a', 'b', 'c', 'd'); //-> ["a", "b", "b", "b", "c", "d"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment