Stream: ideas

Topic: List.slidingWindows


view this post on Zulip Aurélien Geron (Sep 24 2024 at 03:30):

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?

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 03:40):

I don't think it should have start or end. Leave that to sublist called before this function

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 03:42):

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

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 03:42):

I think making the windows then letting the user map or walk should be mostly fine due to seamless slices.

view this post on Zulip Nils Hjelte (Sep 24 2024 at 06:28):

I think List.partition is a nice name for this functionality, https://clojuredocs.org/clojure.core/partition

view this post on Zulip Anton (Sep 24 2024 at 08:00):

We've talked about this before, I think we should add it.

view this post on Zulip Isaac Van Doren (Sep 24 2024 at 12:10):

Partition isn’t quite the same as it produces non overlapping sublists

view this post on Zulip Artem Shamsutdinov (Sep 24 2024 at 13:13):

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

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 13:41):

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.

view this post on Zulip Sky Rose (Sep 24 2024 at 16:58):

Elixir calls this ”chunk", and has several variants: https://hexdocs.pm/elixir/1.12/Enum.html#chunk_every/4

view this post on Zulip Kilian Vounckx (Sep 24 2024 at 19:45):

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