Stream: ideas

Topic: `walk` always getting the index


view this post on Zulip Richard Feldman (Aug 13 2023 at 15:57):

what if instead of having separate functions for walk/walkWithIndex, walkBackwards/walkBackwardsWithIndex we had the "with index" ones be the norm? Like you always get the index as an argument and you can ignore it with _ if you don't care

view this post on Zulip Richard Feldman (Aug 13 2023 at 15:59):

I've noticed I reach for the "with index" versions more often than not when walking, and the index is already computed behind the scenes anyway, so passing it shouldn't have any extra runtime cost (after specialization) if it's unused, because it'll get dead argument eliminated by llvm

view this post on Zulip Richard Feldman (Aug 13 2023 at 16:00):

(in contrast, I find it's the reverse with map - I rarely use the "with index" version, and I often pass a function to map by name, so I don't think it makes sense to be the default for map)

view this post on Zulip Brendan Hansknecht (Aug 13 2023 at 16:02):

That sounds like a good idea. I definitely have a number of cases where I have manually added the index to the state. I think it is pretty common.

view this post on Zulip Agus Zubiaga (Aug 13 2023 at 16:42):

Agreed. Without partial application I see only upsides to this.

view this post on Zulip Richard Feldman (Aug 15 2023 at 06:05):

I went through updating a ton of usages of List.walk in the roc-lang/roc repo, and it seemed like the index was used maybe 5-10% of the time

view this post on Zulip Richard Feldman (Aug 15 2023 at 06:06):

I also noticed that the index is only ever used in any of these examples when going forward and not using Until, so I think having just List.walkWithIndex feels like the right balance for now

view this post on Zulip Richard Feldman (Aug 15 2023 at 06:06):

I'm gonna make a PR to add that and see how it feels

view this post on Zulip Richard Feldman (Aug 15 2023 at 06:30):

https://github.com/roc-lang/roc/pull/5764


Last updated: Jun 16 2026 at 16:19 UTC