Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ramda实现中的Javascript/Typescript分区函数_Javascript_Typescript_Functional Programming - Fatal编程技术网

ramda实现中的Javascript/Typescript分区函数

ramda实现中的Javascript/Typescript分区函数,javascript,typescript,functional-programming,Javascript,Typescript,Functional Programming,见鬼, 我正在为“ramda”中的R.partition实现工作。不用推!! 仅使用map、filter、reduce函数 这是我当前的实现: partition = <T>(func : (param : T) => boolean, arr : T[]): T[][] => { return [arr.filter((element : T) => func(element)), arr.filter((element : T) => !func(elem

见鬼, 我正在为“ramda”中的R.partition实现工作。不用推!! 仅使用map、filter、reduce函数

这是我当前的实现:

partition = <T>(func : (param : T) => boolean, arr : T[]): T[][] => {
return [arr.filter((element : T) => func(element)), arr.filter((element : T) => !func(element))];   
常数温度=[1,2,3,4,5,6,7,8,9];
log(分区(func,tmep));//=>[2,4,6,8],[1,3,5,7,9]

双重过滤会引入不必要的时间复杂性, 我们不需要在这里迭代两次

这是我们的工具:

const partition=(谓词,列表)=>list.reduce(
([left,right],item)=>谓词(item)
?[左。混凝土(项目),右]
:[左,右。混凝土(项目)],
[[], []],
列表
);
//===
常量数据=[1,2,3,4,5,6,7,8,9];
常量isOdd=n=>n%2;
常数[赔率,偶数]=分区(isOdd,数据);

console.log({赔率,evens});
你的问题是什么?我投票结束这个问题,因为这不是一个问题。
const func = (x) => x%2 == 0