简体   繁体   English

使用ramda防止重复代码

[英]prevent duplication code with ramda

i have a list of same operation on same list using ramda like below : 我使用ramda在相同列表上有相同操作的列表,如下所示:

 size: { sum: R.sum(R.map(R.prop('size'), ordersRep)) },
      price: { sum: R.sum(R.map(R.prop('price'), ordersRep)) },
      profit: { sum: R.sum(R.map(R.prop('profit'), ordersRep)) },
      discount: { sum: R.sum(R.map(R.prop('discount'), ordersRep)) },

which i want to define main function : R.sum(R.map(R.prop('somthing'), ordersRep)) other place and use it wheneve needed. 我想定义主要功能: R.sum(R.map(R.prop('somthing'), ordersRep))其他地方并在需要时使用它。 but it's take two argument one list and one prop name. 但是需要两个参数,一个是列表,另一个是道具名称。 how can i handle it ? 我该如何处理?

let combined = (prop, coll) => R.sum(R.pluck(prop, coll))

For an arguably more functional version (point-free courtesy of Ross Mackay): 对于功能更强的版本(Ross Mackay提供免费提示):

let combined = prop => R.compose(R.sum, R.pluck(prop))

let sumPrice = combined('price');
sumPrice([{price: 2}, {price: 3}]); // 5

Point-free: 无积分:

let combined = R.curryN(2, R.compose(R.sum, R.pluck));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM