list - 使用枚举迭代 elixir 中的元组列表
问题描述
我正在尝试通过 Enum.map 遍历元组列表。
coordinates = [{0,0},{1,0},{0,1}]
newcoordinates = Enum.map(coordinates,fn({X,Y})->{X+1,Y+1})
此代码无效。我怎样才能做到这一点 ?
解决方案
首先,您在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}]
推荐阅读
- entity-framework - 如果参数可能为空,则从 sql 选择中获取值
- fonts - Pygame 表面调整大小
- java - AsynchronousSocketChannel 如何保持读取?
- image - 枕头:将 TIFF 从灰度 16 位转换为 8 位会产生全白图像
- sql - 需要指导在数据库中创建父子关系
- regex - 如何在 PowerShell 中使用正则表达式替换
- typescript - 如何判断 rxjs 主题的观察者是否已完成事件处理?
- c++ - 如何强制谷歌基准运行多次迭代?
- javascript - 不断出错 - 尝试播放音频时 - 为什么?
- arrays - 检查子数组是否为山形