我试图了解 Ramda 的传感器。这是来自文档的稍微修改的示例:
const numbers = [1, 2, 3, 4];
const isOdd = (x) => x % 2 === 1;
const firstFiveOddTransducer = R.compose(R.filter(isOdd), R.take(5));
R.transduce(firstFiveOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [ 1, 3, 5, 7, 9 ]
但是如果我想对结果数组的元素求和呢?以下(仅添加R.sum
到R.compose
)不起作用:
const firstFiveOddTransducer = R.compose(R.filter(isOdd), R.take(5), R.sum);