Stream: ideas

Topic: List.split_if


view this post on Zulip Ian McLerran (Feb 17 2025 at 19:04):

What would we think of adding split_if to the List builtin?

IE:

split_if : List a, (a -> Bool) -> List (List a)

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:08):

Can you list the current set of split functions for reference?

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:10):

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:11):

You could also argue that chunks_of belongs in that list.

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:14):

So for example you could do :
chars |> List.split_if(|c| c >= 'A' and c <= 'Z')
instead of:
chars |> List.split_on_list(['A', .. 'Z'])

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:16):

Sounds reasonable.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:17):

Though honestly I'm not sure I like the names of these functions overall....not the clearest....but I don't have concrete suggestions at the moment. Also, not sure what the limit is for number of split function variations. Would be nice to think about a comprehensive set with clean names.

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:18):

Or if you wanted to do something like split_if(chars, |c| !is_alphanumeric(c)), you need to use List.walk. Not the end of the world, but seems like this deserves a split function to me...

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:21):

This doesn't answer your concern about the nomenclature, but if we're concerned about too many split functions, I think split_if is a good replacement for split_on_list, and even split_on if we wanted. split_if can cover both of those use cases.

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:35):

For discussion: a reduced set of more comprehensive/versatile split functions, which covers all current functionality, pus some:

view this post on Zulip Ian McLerran (Feb 17 2025 at 19:37):

This would take us from 5 to 3 split functions, cover all current functionality, and add new functionality.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:09):

I wouldn't depends on List.reverse for now.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:09):

It is expensive

view this post on Zulip Ian McLerran (Feb 17 2025 at 20:09):

Oh, I was thinking that it was just a bit flip... that changes things.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:10):

Not currently

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:10):

Probably will be optimized once we have iterators

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:10):

But no detailed plans yet

view this post on Zulip Ian McLerran (Feb 17 2025 at 20:11):

So then maybe split_first_if/split_last_if?

view this post on Zulip Ian McLerran (Feb 17 2025 at 20:11):

We'd be down to 4 split functions from 5, with more versatility, but not sure if I love those names...

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:22):

Haha, yeah.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 20:22):

Naming is hard

view this post on Zulip Ian McLerran (Feb 18 2025 at 16:31):

Anyone else have thoughts on List splitting functions?

Current Proposed Proposed signature
split_at split_at List a, U64 -> { before: List a, others: List a } # no change
split_on split_if List a, (a -> Bool) -> List (List a)
split_on_list split_if
split_first split_first_if List a, (a -> Bool) -> Result { before: List a, after: List a } [NotFound]
split_last split_last_if List a, (a -> Bool) -> Result { before: List a, after: List a } [NotFound]

Last updated: Jun 16 2026 at 16:19 UTC