roc icon indicating copy to clipboard operation
roc copied to clipboard

`Str.split` and `List.split` have different behavior

Open scott2000 opened this issue 2 years ago • 5 comments

I was using a Str and I wanted to convert my code to work on some ASCII bytes directly with a List U8, so I tried switching from Str.split to List.split, but it didn't work because these functions do different things even though they are both named split. I think this behavior is confusing and inconsistent.

Current functions:

Str.split  : Str,       Str -> List Str
List.split : List elem, Nat -> { before : List elem, others : List elem }

I would expect the current List.split to be named something like List.splitAt, and one of these options for List.split instead:

List.split : List elem, elem           -> List (List elem)
List.split : List elem, List elem      -> List (List elem)
List.split : List elem, (elem -> Bool) -> List (List elem)

It also seems a inconsistent that List.split currently uses .others while List.splitFirst and List.splitLast use .after, but maybe there's a reason for that?

scott2000 avatar Dec 03 '23 15:12 scott2000

If someone takes over this issue, they probably should ask on zulip (contributing or ideas) threads about the preferred naming here.

bhansconnect avatar Dec 03 '23 16:12 bhansconnect

I would be willing to give this a go

ghost avatar Dec 05 '23 22:12 ghost

Looks like this was discussed on Zulip but not implemented

https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/Change.20List.2Esplit.20to.20List.2EsplitAt.3F/near/406667314

Consensus from the thread seems to be leave Str.split as-is and replace List.split with something like

List.splitAt : List a, U64 -> {before: List a, after: List a}
List.splitOn : List a, [Elem a, Sequence (List a)] -> List (List a)

mulias avatar Jun 28 '24 21:06 mulias

I've set up a vote for the last point of doubt.

Anton-4 avatar Jun 29 '24 12:06 Anton-4

In conclusion: We rename the current List.split to List.splitAt and also add:

List.splitOn: List a, a -> List (List a)
List.splitOnSeq: List a, List a -> List (List a)

Anton-4 avatar Jul 01 '24 09:07 Anton-4

I will take a look at this

isaacvando avatar Nov 10 '24 05:11 isaacvando

I'm going to go with List.splitOnList instead of List.splitOnSeq as that seems to be the latest consensus from the thread.

isaacvando avatar Nov 10 '24 05:11 isaacvando