Ramda.map 如何优于 Immutable.List return 列表而不是数组?
How does Ramda.map over Immutable.List return a List instead of an Array?
我在 Immutable.js 中创建了一个地图列表,如下所示:
List [
Map { "id": 0, "name": "John" },
Map { "id": 1, "name": "Lennon" }
]
现在,我已经使用 Ramda 的 R.map 映射了上面的列表:
R.map(map => map.get('id'))(a)
它 returns 一个新的 id 列表,其类型为 Immutable.List
,但不是 JS Array
。
List [ 0, 1 ]
我知道如果定义了 Symbol.iterator
,我们可以遍历任何数据结构。但是返回的类型也是 List
.
这怎么可能?
您获得 List
的原因在 Ramda documentation 中有解释:
map
Dispatches to the map
method of the second argument, if present.
您案例中的第二个参数是 List
。是的,List
有一个 map
方法,如 Immutable documentation:
map
Returns a new List
with values passed through a mapper function.
这就解释了为什么您会得到 List
。
我在 Immutable.js 中创建了一个地图列表,如下所示:
List [
Map { "id": 0, "name": "John" },
Map { "id": 1, "name": "Lennon" }
]
现在,我已经使用 Ramda 的 R.map 映射了上面的列表:
R.map(map => map.get('id'))(a)
它 returns 一个新的 id 列表,其类型为 Immutable.List
,但不是 JS Array
。
List [ 0, 1 ]
我知道如果定义了 Symbol.iterator
,我们可以遍历任何数据结构。但是返回的类型也是 List
.
这怎么可能?
您获得 List
的原因在 Ramda documentation 中有解释:
map
Dispatches to the
map
method of the second argument, if present.
您案例中的第二个参数是 List
。是的,List
有一个 map
方法,如 Immutable documentation:
map
Returns a new
List
with values passed through a mapper function.
这就解释了为什么您会得到 List
。