Stream: ideas

Topic: Builtins, in List add: concat_str, keep_until, keep_while


view this post on Zulip removewingman (Sep 05 2026 at 08:12):

Proposal new Builtins:

concat_str : List(Str), Str -> Str
concat_str = |items, seperator| {
  items.fold_with_index("", |state, item, index|{
    if index == 0 state.concat(item) else state.concat(seperator).concat(item)
  })
}
expect [] |> concat_str(",") |> Str.is_eq("")
expect [ "A" ] |> concat_str(",") |> Str.is_eq("A")
expect [ "A", "B" ] |> concat_str(",") |> Str.is_eq("A,B")
expect [ "A", "B", "C" ] |> concat_str(",") |> Str.is_eq("A,B,C")

keep_until : List(item), (item -> Bool) -> List(item)
keep_until = |items, predicate| {
    var $result = []
    for item in items {
        if predicate(item) == False {
            $result = $result.append(item)
        } else return $result
    }
    $result
}
expect [1, 2, 3] |> keep_until(|item| item == 2) |> List.is_eq([1])
expect [1, 2, 3] |> keep_until(|item| item > 2) |> List.is_eq([1, 2])
expect [1, 2, 3] |> keep_until(|item| item > 0) |> List.is_empty()

keep_while : List(item), (item -> Bool) -> List(item)
keep_while = |items, predicate| {
    var $result = []
    for item in items {
        if predicate(item) {
            $result = $result.append(item)
        } else return $result
    }
    $result
}
expect [1, 2, 3] |> keep_while(|item| item == 1) |> List.is_eq([1])
expect [1, 2, 3] |> keep_while(|item| item < 3) |> List.is_eq([1, 2])
expect [1, 2, 3] |> keep_while(|item| item < 1) |> List.is_empty()

Similar implementations in OCaml: https://ocaml.org/manual/5.5/api/String.html#VALconcat, https://ocaml.org/manual/5.5/api/List.html#VALdrop_while, https://ocaml.org/manual/5.5/api/List.html#VALtake_while

The implementation can be changed, I care about the signature, any thoughts or resistance against implementing these?

view this post on Zulip Anton (Sep 05 2026 at 12:04):

Hi @removewingman,
We already have concat_str, it is called Str.join_with, feel free to add it to https://github.com/roc-lang/www.roc-lang.org/blob/681d586d6b2f2c821592bca7475f2ce2e495bc19/website/content/different-names.md#L4 though

view this post on Zulip Anton (Sep 05 2026 at 12:08):

keep_until and keep_while look good :+1:


Last updated: Sep 24 2026 at 15:59 UTC