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?
concat_str : List(Str), Str -> Strconcat_str : label~Str:"", List(Str) -> Str or something like that, if this is still possible [ "A", "B", ""C ].concat(",")keep_until : List(item), (item -> Bool) -> List(item)keep_while : List(item), (item -> Bool) -> List(item)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
keep_until and keep_while look good :+1:
Last updated: Sep 24 2026 at 15:59 UTC