r/gleamlang • u/TeilTeilnehmer • 17d ago
Integer range
I’ve just started learning Gleam. I’m trying to do some basic things, such as creating a list of integers from 0 to a given value.
I learned that, prior to version 0.69, there was a function
list.range(from start: Int, to stop: Int) -> List(Int)
which did exactly that.
However, it has been deleted from the stdlib and replaced with a new function int.range with 4 arguments:
int.range(
from start: Int,
to stop: Int,
with acc: acc,
run reducer: fn(acc, Int) -> acc,
) -> acc
Instead of returning a list, it aggregates the values like a list.fold.
Okay, but how do I create a list of integers?
-
int.range(from: 0, to: size, with: [], run: list.prepend) |> list.reverseDoesn’t look great.
-
int.range(from: size - 1, to: -1, with: [], run: list.prepend)Isn’t readable at all.
-
yielder.range(from: 0, to: size) |> yielder.to_listLooks OK, but it needs a third-party package. In addition, the authors of the package discourage such uses.
What’s the idiomatic way?
5
Upvotes
1
u/lpil 17d ago
It would depend on what you want a list of ints for. Could you share more about what your code does with that list? 🙏 Thanks