我想做FP等效的(伪代码):
var l1 = [ '1', '2', '3' ];
var l2 = [ 'a', 'b', 'c' ];
var table = []
for i in l1 {
var row = [];
for j in l2 {
row.push([i, j]);
}
table.push(row);
}
导致
table ==
:
[
[["1", "a"], ["1", "b"], ["1", "c"]],
[["2", "a"], ["2", "b"], ["2", "c"]],
[["3", "a"], ["3", "b"], ["3", "c"]]
]
我通过以下方式实现了这一目标:
const headEquals = (x,y) => R.equals(R.head(x), R.head(y));
const transform = R.compose(
R.groupWith(headEquals),
R.xprod
);
const table = transform(l1,l2);
。。。但它感觉很不雅观——由
xprod
正在从输入中丢弃结构,然后通过执行大量
equals
比较。
在拉姆达实现这一目标的更好方法是什么?