Stream: beginners

Topic: ✔ List with default


view this post on Zulip Kiryl Dziamura (Jan 23 2024 at 12:22):

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

view this post on Zulip JRI98 (Jan 23 2024 at 12:41):

There is List.repeat https://www.roc-lang.org/builtins/List#repeat

view this post on Zulip Notification Bot (Jan 23 2024 at 12:47):

Kiryl Dziamura has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC