Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
Functional programming 请问如何使用ramda实现以下功能_Functional Programming_Ramda.js - Fatal编程技术网

Functional programming 请问如何使用ramda实现以下功能

Functional programming 请问如何使用ramda实现以下功能,functional-programming,ramda.js,Functional Programming,Ramda.js,我在ramdom中有一个由1到5个数字组成的随机数组,有时[1,1,1,1,2,2]等等。我的任务是寻找始终出现频率最高的值。我使用javascript实现了这一点,如下所示,使用了一个名为ramda的库。在阅读了文档之后,我提出了如下解决方案 // filter out duplication in array that way you can get the uniq represented numbers const uniqueItems = R.uniq(params); // us

我在ramdom中有一个由1到5个数字组成的随机数组,有时[1,1,1,1,2,2]等等。我的任务是寻找始终出现频率最高的值。我使用javascript实现了这一点,如下所示,使用了一个名为ramda的库。在阅读了文档之后,我提出了如下解决方案

// filter out duplication in array that way you can get the uniq represented numbers
const uniqueItems = R.uniq(params);

// use the unique numbers as keys and create a new array of object
 const mappedItemsWithRepresentations = map((a) => ({ color: a, rep: params.filter(b => b === a).length }), uniqueItems);

// and then finally, select the item with highest rep and return it key
const maxRepItem = mappedItemsWithRepresentations.reduce((acc, curr) => acc.rep > curr.rep ? acc : curr, []);

return maxRepItem.key; // gives me the correct value i need
然而,通过阅读文档中的更多内容和示例,我意识到有一种方法可以将上面的逻辑与ramda结合起来。我尝试了许多可能的尝试,我能得到的最接近的是下面

const getMaxRep = curry(pipe(uniq, map((a) => ({ color: a, rep: filter(b => b === a).length })), pipe(max(pathEq("rep")), tap(console.log))));

console.log("Max Rep here", getMaxRep(params));

我还试着使用简化功能,但都没有用。请问我该如何安排才能做到这一点?任何帮助都将不胜感激。

拉姆达有R.countBy来获取发生的次数。您可以将country的结果对象转换为pairs[值,计数],然后将其减少以找到计数最高的一对:

const{pipe,countBy,identity,toPairs,reduce,maxBy,last,head}=R
常数fn=管道(
countBy(identity),//计算发生的次数
toPairs,//转换为[值,计数]对
reduce(maxBy(last),[0,0]),//reduce查找最大出现次数
head,//获取实际值
Number,//转换回一个数字
)
常数arr=[1,1,1,2,2]
常数结果=fn(arr)
console.log(结果)

拉姆达使用R.countBy来获取出现次数。您可以将country的结果对象转换为pairs[值,计数],然后将其减少以找到计数最高的一对:

const{pipe,countBy,identity,toPairs,reduce,maxBy,last,head}=R
常数fn=管道(
countBy(identity),//计算发生的次数
toPairs,//转换为[值,计数]对
reduce(maxBy(last),[0,0]),//reduce查找最大出现次数
head,//获取实际值
Number,//转换回一个数字
)
常数arr=[1,1,1,2,2]
常数结果=fn(arr)
console.log(结果)

很好的用例,请尝试以下方法:

const maxReduce=reduce(maxBy(最后一个),[0,0])
const getMaxRep=管道(countBy(identity)、toPairs、maxReduce、head)
log(getMaxRep([1,1,1,1,2,2]))

countBy是一个非常好的开始,遗憾的是Ramda不支持对象的reduce,但我们可以使用toPairs函数转换为数组并完成这项工作。

很好的用例,请尝试以下操作:

const maxReduce=reduce(maxBy(最后一个),[0,0])
const getMaxRep=管道(countBy(identity)、toPairs、maxReduce、head)
log(getMaxRep([1,1,1,1,2,2]))

countBy是一个非常好的开始,遗憾的是Ramda不支持对象的reduce,但是我们可以使用toPairs函数转换为数组并完成这项工作。

我不完全清楚您要的是什么

但可能是这样的:

const maxRep=管道(
伯爵(身份),
托帕尔斯,
地图(zipObj(['color','rep']),
reduce(maxBy(prop('rep')),{rep:-无穷大}),
)
常量参数=[1,2,3,4,2,3,5,2,3,2,2,1,1,4,5,5,3,2,5,1,5,2]
控制台日志(
maxRep(参数)
)


const{pipe,countBy,identity,toPairs,map,zipObj,reduce,maxBy,prop}=R
我不完全清楚你要的是什么

但可能是这样的:

const maxRep=管道(
伯爵(身份),
托帕尔斯,
地图(zipObj(['color','rep']),
reduce(maxBy(prop('rep')),{rep:-无穷大}),
)
常量参数=[1,2,3,4,2,3,5,2,3,2,2,1,1,4,5,5,3,2,5,1,5,2]
控制台日志(
maxRep(参数)
)


const{pipe,countBy,identity,toPairs,map,zipObj,reduce,maxBy,prop}=R
Plz从上面的代码中,我认为head的意思是无论如何都要获取第一个值,在这种情况下,它如何转换为获取实际值。我正在努力更好地理解代码。
头从
[“1”,4]
条目中获取第一个值,得到“1”。然后我们应用
Number
将其转换回一个数字。减少的结果是[“1”,“4”],我们需要第一个项目,因此是
标题
。scott更快:)根据上面的代码,我认为head的意思是获取第一个值,在这种情况下,如何转换为获取实际值。我正在努力更好地理解代码。
头从
[“1”,4]
条目中获取第一个值,得到“1”。然后我们应用
Number
将其转换回一个数字。减少的结果是[“1”,“4”],我们需要第一个项目,因此是
标题
。斯科特跑得更快:)这帮了大忙,先生。谢谢你的时间,这很有帮助,先生。谢谢你的时间