Javascript Ramda-按索引划分

Javascript Ramda-按索引划分,javascript,ramda.js,Javascript,Ramda.js,如何通过使用索引实现分区 /* *@param{number}索引 *@param{[]}列表 *@returns{[found,rest[]}-数组,其索引0包含找到的元素 **索引1包含给定列表的其余部分 */ const partitionByIndex=(索引,列表)=>{}; //这是我目前得到的,但我真的认为它太冗长了 export const partitionByIndex=R.curry((i,卡片)=>R.pipe( R.划分(R.等于(R.nth(i,卡片)), ([f

如何通过使用
索引
实现
分区

/*
*@param{number}索引
*@param{[]}列表
*@returns{[found,rest[]}-数组,其索引0包含找到的元素
**索引1包含给定列表的其余部分
*/
const partitionByIndex=(索引,列表)=>{};
//这是我目前得到的,但我真的认为它太冗长了
export const partitionByIndex=R.curry((i,卡片)=>R.pipe(
R.划分(R.等于(R.nth(i,卡片)),
([found,rest])=>[R.head(found),rest],
)(卡片);
常量列表=[1,2,3,4,5,6,7];
常数指数=1;
const[found,rest]=partitionByIndex(索引,列表);
log({found,rest})

另一种方法是在
R.nth
元素周围
R.take
R.drop
,例如:

R.converge(R.pair, [R.nth, (n, xs) => R.concat(R.take(n, xs), R.drop(n + 1, xs))])
或者没有拉姆达:

(n, xs) => [xs[n], xs.slice(0, n).concat(xs.slice(n + 1))]

一个相当简单的无点解决方案是

const partitionByIndex = converge(pair, [compose(of, nth), flip(remove)(1)])

partitionByIndex(2, ['a', 'b', 'c', 'd', 'e']) //=> [['c'], ['a', 'b', 'd', 'e']]
flip
让我意识到
remove
的参数可能顺序错误

更新 Yogi指出,期望的反应可能是
['c',['a','b','d','e']
,而不是上面的结果:
['c'],['a','b','d','e']
。如果是这样的话,这段代码可以变得更简单:

const partitionByIndex = converge(pair, [nth, flip(remove)(1)])

partitionByIndex(2, ['a', 'b', 'c', 'd', 'e']) //=> ['c', ['a', 'b', 'd', 'e']]

因为他们不需要将找到的值作为列表,所以
收敛(pair[nth,flip(remove)(1)])
就足够了吗?@Yoshi:也许吧。我可能没有正确地解释这个问题。这其实是我的第一个想法。由于我对分区工作的看法,我改为上面的内容。但是看看代码尝试,我认为你是对的。更新了答案