Is there a quick way to init a list of a given length, populated with default values?
I think withCapacity
+ append
should work fine, but it's a bit verbose for this fairly common task (range
+ map
feels even worse).
Maybe there's already something for this?
For now, I'm using this:
listWithDefault : Nat, a -> List a
listWithDefault = \len, default ->
List.range { start: At 0, end: Before len }
|> List.map \_ -> default
There is List.repeat
https://www.roc-lang.org/builtins/List#repeat
Kiryl Dziamura has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC