barbiturat
8/4/2017 - 5:25 PM

## small ramda helpers

small ramda helpers

``````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
]);

////////////////////////////////////////////////////////////////

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"]

``````