javascript - ramda equivalent for rotate -
given rotate function 1 below, rotates array set number of slots.
is there equivalent ramda.js function or composition rotation?
var test = [1,2,3,4,5,6,7,8,9]; function rotate(arr, count) { arr = arr.slice(); while (count < 0) { count += arr.length; } count %= arr.length; if (count) { arr.splice.apply(arr, [0, 0].concat([].slice.call(arr.splice(arr.length - count, count)))); } return arr; }
example
rotate(test, 2) // -> [8, 9, 1, 2, 3, 4, 5, 6, 7]
here's point-free 1 liner takes count first , data second, consistent ramda's composable style:
const rotate = pipe(splitat, reverse, flatten);
of course can flip(rotate)
data first version.
update
sorry, read fast , assumed normal, left-wise direction rotation (eg, in ruby). here's variation of idea original does:
const rotate = pipe(usewith(splitat, [negate, identity]), reverse, flatten);
Comments
Post a Comment