Stream: beginners

Topic: descending range function


view this post on Zulip Norbert Hajagos (Mar 07 2026 at 15:40):

Hi all! I've found the range_to and range_until functions in the builtins, which the numbers use in their to and until methods, but they only go in ascending direction. I would need a descending version of that to use it in a for loop for n in [5, 4, 3, 2, 1, 0]. I think this is a pretty common operation. How we'd want that to look like ideally?

  1. Built in method like 5.down_to(0)
  2. 0.to(5).reverse(), where "something-something iterators will solve the perf problem"
  3. Adives to use a while loop.
  4. something else?

view this post on Zulip Brendan Hansknecht (Mar 07 2026 at 17:48):

Should range_to and range_until just also work in reverse? 5.range_to(0)? I guess maybe that is more accidentally error prone. 1.range_to(x) (unclear if forward or reverse).

view this post on Zulip Richard Feldman (Mar 07 2026 at 19:06):

in the future I want ranges to return iterators, which can be reversed

view this post on Zulip Richard Feldman (Mar 07 2026 at 19:06):

so that would take care of all of those use cases hopefully!

view this post on Zulip Richard Feldman (Mar 07 2026 at 19:06):

we don't have Iter in builtins yet though

view this post on Zulip Norbert Hajagos (Mar 07 2026 at 20:48):

Shall I implement a rev / reverse / reversed method on List in pure Roc, so that the api is there? I vaguely remember you wanting to implement iterators in a way where the user would interact mainly / exclusively with Lists, but that could have changed.

view this post on Zulip Richard Feldman (Mar 07 2026 at 21:15):

yeah List.rev sounds like a good thing to have anyway!


Last updated: Mar 20 2026 at 12:28 UTC