【发布时间】:2020-08-16 14:30:47
【问题描述】:
我是函数式编程的新手,我想浏览一个集合并根据条件找到一个元素。条件如下,但我想知道是否有更优雅的方式以函数方式编写它(下面使用 Ramda):
import * as R from "ramda";
const data = [{x: 0, y: 0} , {x: 1, y: 0}];
//return the cell which matches the coord on the given orientation
function findCell(orientation, coord) {
const search = R.find(cell => {
if (orientation === "x") {
return cell.x === coord;
} else {
return cell.y === coord;
}
});
return search(data);
}
findCell("x", 0);
有没有更优雅的方式在 Ramda 或其他一些函数式 JS 库中编写这个谓词?
【问题讨论】:
标签: javascript functional-programming ramda.js