Can someone please provide a suggestion on how to iterate a list BUT with a batch of x at a time?
If the functionality existed:
["1","2","3","4","5","6","7","8","9","10"].step(5)|> IO.puts
Would produce in two iterations:
12345
678910
I believe Stream.iterate/2 is the solution but my attempts to do so given an array are not proving profitable.
Enum.chunk_every/2 (or Stream.chunk_every/2) will break a list down into sublists of x elements:
iex> [1,2,3,4,5,6,7,8,9,10] |> Enum.chunk_every(5)
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
You can then act on each list, for example:
iex> ["1","2","3","4","5","6","7","8","9","10"]
|> Enum.chunk_every(5)
|> Enum.each(fn x -> IO.puts x end)
12345
678910
Both Enum.chunk/2 and Enum.chunk/2 are deprecated.
Use Enum.chunk_every/2 and Stream.chunk_every/2 instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With