以功能方式列出具有两个相邻元素的遍历



我正在尝试实现以下代码的功能版本

 const adjacent = (list)  => {
    let results = [];
    for (let idx = 0; idx < list.length - 1; idx++) {
      const computedRes = someComplexFn(list[idx], list[idx + 1]);
      results.push(computedRes );
    }
    return results;
  }

我随附以下版本

const locations = [1,2,3,4,5];
const calcRatioFn = (x, y) => x+y;
const adjacentMap = (list, result=[]) => {
  if(R.length(list) < 2) {
    return result;
  }
  const f1 = R.head(list);
  const f2 = R.tail(list);
  result.push(calcRatioFn(f1 ,R.head(f2)));
  return adjacentMap(R.tail(list), result);
}

const results = adjacentMap(locations);
console.log(results);

上述还有其他简单解决方案吗?

我们可以避免默认结果值参数,如果条件从上述函数检查?

jsbin链接http://jsbin.com/veyihepulu/1/edit?html,JS,Console

一种方法是使用R.aperture创建相邻元素的滑动窗口。然后,可以用R.apply包装一些额外的糖someComplexFn,以将二进制函数转换为一个接受两个元素的数组。

您的示例将看起来像:

const adjacentMap = R.pipe(R.aperture(2), (R.map(R.apply(someComplexFn))))

另一种方法是在数组上使用 converge,而无需一个没有第一个元素的数组。

let locations = [1,2,3,4,5];
const calcRatio = (x, y) => x+y;
// adjacentMap :: Array -> Array
const adjacentMap = R.converge(
    R.zipWith(calcRatio), 
    [ R.init, R.tail]
);
// saveAdjacentMap :: Array -> Array
const saveAdjacentMap = R.cond([
    [R.compose(R.lt(1), R.length), adjacentMap ],
    [R.T, R.identity]
]);
console.log(saveAdjacentMap(locations));

您的JSBIN使用RAMDA 0.8.0。当前版本0.24.1

发生了变化

以下代码也许您需要或可以适应所需的解决方案。

const fn = (acc, c, i, a) => {
  return !(a[i + 1]) 
    ? acc
    : acc.concat(c + a[i + 1])
}
const _adjacentMap = (fn, list) => {
  return list.reduce(fn, [])
}
const locations = [1,2,3,4,5]
const result = _adjacentMap(fn, locations)
console.log(result)
// => [ 3, 5, 7, 9 ]

最新更新