繁体   English   中英

嵌套数组中的Ramda索引

[英]Ramda indices in nested arrays

试图掌握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秒将变为-1

我知道Ramda可以使用R.addIndex扩展R.map:const const mapWithIndex = R.map(R.addIndex)所以mapWithIndex((i, value) => // do something))可以访问每个的索引但我确定如何将所有参数传递给最低级别

我甚至无法将i和j加在一起,尝试过类似的东西:

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

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

我错过了什么? 任何帮助将非常感激 :)

有两个问题:

  • 内部mapWithIndex需要访问row以便它可以遍历切片
  • 您的示例代码中的值和索引参数相反

这应该这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM