【发布时间】:2019-03-20 09:06:08
【问题描述】:
我正在尝试通过 Enum.map 遍历元组列表。
coordinates = [{0,0},{1,0},{0,1}]
newcoordinates = Enum.map(coordinates,fn({X,Y})->{X+1,Y+1})
此代码无效。我该怎么做?
【问题讨论】:
标签: list tuples elixir enumeration
我正在尝试通过 Enum.map 遍历元组列表。
coordinates = [{0,0},{1,0},{0,1}]
newcoordinates = Enum.map(coordinates,fn({X,Y})->{X+1,Y+1})
此代码无效。我该怎么做?
【问题讨论】:
标签: list tuples elixir enumeration
首先,您在函数声明后缺少end。其次,在 Elixir 中,以大写字母开头的标识符是原子,小写字母是变量,不像 Erlang 中大写字母是变量,小写字母是原子。所以你只需要把它们变成小写:
iex(1)> coordinates = [{0, 0},{1, 0},{0, 1}]
[{0, 0}, {1, 0}, {0, 1}]
iex(2)> newcoordinates = Enum.map(coordinates, fn {x, y} -> {x + 1, y + 1} end)
[{1, 1}, {2, 1}, {1, 2}]
【讨论】:
【讨论】: