Iterating over consecutive pairs in a List is a pretty common pattern. Right now, this is how I code it (you may have a better idea?):
list = [0, 10, 20, 30, 40, 50]
result = List.map2 list (list |> List.sublist { start: 1, len: (List.len list - 1)}) \a, b -> (a, b)
expect result == [(0, 10), (10, 20), (20, 30), (30, 40), (40, 50)]
It's not trivial to come up with this, and it's pretty hard to read. Perhaps we could have a List function for that? For example:
list = [0, 10, 20, 30, 40, 50]
result = list |> List.slidingWindows { size: 2 } \window -> window
expect result == [[0, 10], [10, 20], [20, 30], [30, 40], [40, 50]]
We could also provide a few more optional arguments:
list = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = list |> List.slidingWindows { size: 3, start: At 1, end: Before 9, step: 2, keepRemainder: Bool.true} \window -> window
expect result == [[10, 20, 30], [30, 40, 50], [50, 60, 70], [70, 80]]
In this example, List.slidingWindows acts like List.map but it maps over windows. Perhaps it should instead walk over the windows? We would need to add a state in this case. Or perhaps we could have both List.mapSlidingWindows and List.walkSlidingWindows.
Wdyt?
I don't think it should have start or end. Leave that to sublist called before this function
Also, not sure the standard on the edge slices. I feel like most common, all slices would be the exact same size and edges would be ignored... But not sure fully
I think making the windows then letting the user map or walk should be mostly fine due to seamless slices.
I think List.partition is a nice name for this functionality, https://clojuredocs.org/clojure.core/partition
We've talked about this before, I think we should add it.
Partition isn’t quite the same as it produces non overlapping sublists
Isaac Van Doren said:
Partition isn’t quite the same as it produces non overlapping sublists
I would also guess that from the name alone, but in clojure it's possible to overlap if the step is smaller than the partition size
another advantage is that partition is a short descriptive word that can encompass all this related functionality
in my opinion clojure api is very nice, though it obviously needs adjustment for how the things are done in Roc
Personally I think window or slidingWindow is a much better name than partition. Partition has a lot of assumptions in my experience related to various possible algorithms and designs (for example partitioning for sorting).
That said, at first glance, closure API seems reasonable (thought would need to pay attention to how padding works more). Also, probably a great API to return an iterator once we have them.
Elixir calls this ”chunk", and has several variants: https://hexdocs.pm/elixir/1.12/Enum.html#chunk_every/4
There is already List.chunksOf, but maybe this needs to be removed to have a consistent api
Last updated: Jun 16 2026 at 16:19 UTC