Elixir Step List

Can anyone suggest a suggestion on how to iterate over a list, but at intervals of x at a time?

For instance:

If functionality existed:

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

It is produced in two iterations:

12345

678910

I believe Stream.iterate / 2 is a solution, but my attempts to do this with an array are not profitable.

+6
source share
1 answer

Enum.chunk / 2 (or Stream.chunk / 2 ) will break the list down to a sublist of x elements:

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

Then you can act on each list, for example:

 iex> ["1","2","3","4","5","6","7","8","9","10"] |> Enum.chunk(5) |> Enum.each(fn x -> IO.puts x end) 12345 678910 
+11
source

Source: https://habr.com/ru/post/987581/


All Articles