elixir enum chunk_every
To chunk a list in Elixir using the Enum.chunk_every/2
function, you can provide the list and the desired chunk size as arguments. The function will return a list of sublists, where each sublist contains the specified number of elements from the original list.
Here's an example of how to use Enum.chunk_every/2
in Elixir:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
result = Enum.chunk_every(list, chunk_size)
In this example, the list
variable contains the elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
, and the chunk_size
variable is set to 3
. The Enum.chunk_every/2
function is then called with these arguments, resulting in the following value for result
:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
The original list is divided into sublists of size 3, except for the last sublist, which contains the remaining elements.
Please note that the Enum.chunk_every/2
function is available in Elixir version 1.6 and later.