[Proposal] offset argument for Enum.chunk_every

19 views
Skip to first unread message

Konstantin Astafurov

unread,
Aug 13, 2022, 11:09:38 PM8/13/22
to elixir-lang-core
Currently, Enum.chunk_every starts chunking at the start of the list. So it's impossible to generate chunks with "leftovers" on both sides without extra tinkering. I propose adding an offset argument (the index from which the first chunk would start) to the Enum.chunk_every function.

so [1,2,3,4,5] with count 3 step 2 and offset of -2 would return
[1], [1,2], [1,2,3], [2,3,4], [3,4,5], [4,5], [5]

Elements in the leftover option would be to the left of the initial chunks less than count. It'd also be neat if you could have different leftover options for the left-hand and right-hand leftovers.

Some of my potato code where this feature could be used - it's one way of implementing Conway's Game of Life logic. See setup_arr.

Enum.chunk_every(row, 3, 1, -1, [0]) in place of the first setup_arr
Reply all
Reply to author
Forward
0 new messages