嵌套数组中的Ramda索引

Ramda indices in nested arrays

本文关键字:Ramda 索引 数组 嵌套      更新时间:2023-09-26

到目前为止,我一直在努力掌握Ramda并喜欢它,但我正在为一些看起来很基本的东西而挣扎(很抱歉,如果这是重复的)

我有一个阵列阵列(代表一个游戏板)

const board = [
  [4, 0, 2, 2, 0, 3, 0, 2],
  [4, 3, 3, 1, 2, 3, 4, 3],
  [4, 4, 3, 2, 4, 1, 1, 4],
  [0, 2, 4, 1, 0, 3, 2, 2],
  [4, 1, 0, 1, 2, 2, 4, 1],
  [3, 3, 4, 3, 2, 0, 1, 3],
  [2, 2, 4, 2, 2, 1, 2, 2],
  [3, 3, 2, 3, 1, 1, 2, 3]
]

玩家用匹配的瓦片建立移动,所以类似于:

const move = [[7, 4], [7, 5], [6, 5]]

我想将移动坐标匹配的瓦片映射到不同的值(例如,这些坐标的1 s将变为-1

我知道Ramda可以用R.addIndex:const mapWithIndex = R.map(R.addIndex)来增强R.map,这样mapWithIndex((i, value) => // do something))就可以访问每个瓦片的索引,但我知道如何将所有参数传递到最低级别的

我甚至不能把I和j加在一起,我试过这样的东西:

const mapWithIndex = R.addIndex(R.map)
const addIndices = mapWithIndex((i, row) => mapWithIndex((j, tile) => j + i))

但是调用这个函数会返回一个函数数组。

我错过了什么?如有任何帮助,我们将不胜感激:)

有两个问题:

  • 内部CCD_ 5需要访问CCD_
  • 示例代码中的值和索引参数颠倒了

这应该做到:

const mapWithIndex = R.addIndex(R.map);
const addIndices = mapWithIndex((row, i) => mapWithIndex((tile, j) => j + i)(row));