使用Ramda和无点样式,我如何将数组的第一项复制到它的末尾

Using Ramda, and pointfree style, how can I copy the first item of an array to the end of it?

本文关键字:复制 一项 数组 Ramda 样式 使用      更新时间:2023-09-26

我想取一个数组[1, 2, 3]并返回[1, 2, 3, 1]

我正在使用Ramda,我可以像这样得到想要的结果:

const fn = arr => R.append(R.prop(0, arr), arr);

但我想做点自由。这是我得到的最接近的:

const fn = R.compose(R.append, R.prop(0));
fn(arr)(arr)

但是那看起来很傻。我错过了什么?谢谢!

converge对于这样的事情非常有用。

const rotate = R.converge(R.append, [R.head, R.identity])
rotate([1, 2, 3]); //=> [1, 2, 3, 1]

S组合子在这里很有用:

S.S(S.C(R.append), R.head, [1, 2, 3]);
// => [1, 2, 3, 1]