如何使用Ramda通过索引对n数组求和

How to sum n-arrays by index using Ramda

本文关键字:数组 求和 索引 何使用 Ramda      更新时间:2023-09-26

我一直在学习Ramda,想知道如何按索引对n-数组求和。以下是我对2个数组所做的操作。我如何使这种方法具有规模?

即,我希望能够做到这一点:sumByIndex( arr1, arr2, ..., arrn )

给定列表xy,所得数组应产生[x0 + y0, x1 + y1, ..., xn + yn]。因此,对于n-array的情况,得到的数组应该是[ a[0][0] + a[1][0] + ... a[n][0], a[0][1] + a[1][1] + ... a[n][1], ..., a[0][n] + a[1][n] + ... + a[n][n] ],其中a[n]是一个数组,作为位置n处的自变量。

var array1 = [1,2,3];
var array2 = [2,4,6];
var sumByIndex = R.map(R.sum);
var result = sumByIndex(R.zip(array1, array2));
$('pre').text(JSON.stringify(result, true));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.18.0/ramda.min.js"></script>
<pre></pre>

为了实现这一点,我们将从创建一些通用的辅助函数开始:

// a new version of `map` that includes the index of each item
var mapI = R.addIndex(R.map);
// a function that can summarise a list of lists by their respective indices
var zipNReduce = R.curry(function(fn, lists) {
  return mapIndexed(function (_, n) {
    return fn(R.pluck(n, lists));
  }, R.head(lists));
});

一旦我们有了这些,我们就可以通过将R.sum传递给上面定义的zipNReduce来创建sumByIndex

var sumByIndex = zipNReduce(R.sum);
sumByIndex([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [12, 15, 18]

如果您更喜欢创建一个函数,该函数接受不同数量的数组作为参数,而不是数组数组,那么您可以简单地用R.unapply:包装它

var sumByIndex_ = R.unapply(sumByIndex);
sumByIndex_([1, 2, 3], [4, 5, 6], [7, 8, 9]); // [12, 15, 18]

如果你可能要处理不同大小的列表,我们可以用一个轻微的变化来替换R.sum,默认未定义的值为零:

var sumDefaultZero = R.reduce(R.useWith(R.add, [R.identity, R.defaultTo(0)]), 0);
var sumByIndexSafe = zipNReduce(sumDefaultZero);
sumByIndexSafe([[1, 2, 3], [], [7, 9]]); // [8, 11, 3]

我发现答案有点冗长。最好保持简单。

import { compose, map, unnest, zip, sum } from 'ramda';
const a = [1,2,3]
const b = [4,5,6]
const c = [7,8,9]
function groupByIndex(/*[1,2,4], [4,5,6], ...*/) {
  return [...arguments].reduce(compose(map(unnest), zip));
}
const sumByIndex = map(sum);
const res = sumByIndex(groupByIndex(a,b,c))
// => [12,15,18]

我来晚了一点,但假设所有数组的长度相同,我们可以将第一个数组作为归约函数的初始值。剩下的部分用一个zipWith函数迭代,该函数将两个数字相加。

const {unapply, converge, reduce, zipWith, add, head, tail} = R;
const a = [1,2,3];
const b = [4,5,6];
const c = [7,8,9];
var zipSum = 
  unapply(
    converge(
      reduce(zipWith(add)), [
        head,
        tail]));
var res = zipSum(a, b, c);
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>