IO.put-6ren">
gpt4 book ai didi

elixir - 步骤列表与 Elixir

转载 作者:行者123 更新时间:2023-12-04 13:49:20 30 4
gpt4 key购买 nike

有人可以提出一个建议,如何一次用一批x迭代列表BUT吗?

例如:

如果功能存在:

["1","2","3","4","5","6","7","8","9","10"].step(5)|> IO.puts

将产生两次迭代:

12345

678910



我相信Stream.iterate/2是解决方案,但鉴于 数组的尝试,我的尝试并没有证明是有利可图的。

最佳答案

Enum.chunk/2(或Stream.chunk/2)会将列表分解为x个元素的子列表:

iex> [1,2,3,4,5,6,7,8,9,10] |> Enum.chunk(5)                   
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]

然后,您可以对每个列表进行操作,例如:
iex> ["1","2","3","4","5","6","7","8","9","10"]
|> Enum.chunk(5)
|> Enum.each(fn x -> IO.puts x end)
12345
678910

关于elixir - 步骤列表与 Elixir ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351847/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com