It definetly needs more cleanup: https://gist.github.com/FabHof/d65eab72dceca521642e90f8e5dd5ad7
But I did find a bug with dbg: https://github.com/roc-lang/roc/issues/5883
https://github.com/mulias/roctoberfest/blob/main/advent_2022/day_03/main.roc
List.partition : List a, Nat -> List (List a)
would be a reasonable addition to the builtins, it's something that comes up frequently, at least in AoC, and I couldn't find it.List.sum
tripped me up, I waned to sum up a list of U8
s into an U64
but got a type error. I ended up doing priorities |> List.map Num.intCast |> List.sum
.roc-parser
, works like :butter:Yes, partition would be great
What is partition in this case? Like what does the function do?
What I would expect is that it would break the input list into chunks of size n. With the last chunk potentially containing less than n elements.
Here's what I did
partition : List a, Nat -> List (List a)
partition = \lst, idx ->
{ before, others } = List.split lst idx
if List.isEmpty others then
[before]
else
List.prepend (partition others idx) before
expect partition [1, 2, 3, 4, 5, 6, 7, 8] 3 == [[1, 2, 3], [4, 5, 6], [7, 8]]
Ah, that form of partition.
I wonder if there is a better name for it.
Yes, not sorting partition. Uhhhh, chunk
?
Or possibly chunksOf
Personally partition is what I would think of first if I was searching for it
I think Brendan is right that partition is overloaded, since partitioning in half/around a pivot point is a common operation in sorting.
In the haskell split package this is called chunksOf
, that seems like a great name.
List.sum tripped me up, I waned to sum up a list of U8s into an U64 but got a type error.
I think making that work could be very complicated
Anton said:
I think making that work could be very complicated
sure sure. It wasn't a particularly constructive comment, mostly that I'm not used to a functional language caring so much about number encodings.
I think we should look at adding automatic upcasting. If integer and the same sign with more bits, just automatically convert. This could be done automatically during the underlying list walk that sum should be calling. That will also be more performance than making the entire list first
Interesting!
Last updated: Jul 06 2025 at 12:14 UTC