I'm interested to know what utility functions people commonly use when writing advent of code solutions (or anything else). I'm thinking along the lines of simple things which are somewhat general-purpose rather than very specific to the kind of puzzles you get. Mine are:
three = [[1, 2], [3]] |> sumBy List.len
This one feels quite intuitive to use. On the other hand, it's not much more concise than map
+ sum
. I learnt FSharp for a while before Roc, so I became use to these 'map+' functions (see also sortBy). As I understand it the performance reasons for wanting this kind of function may be negated by future language features like iterators?
upsert dict key 1 (\value -> value + 1)
The alternative seems quite verbose:
Dict.update dict key \current ->
when current is
Ok value -> Ok (value + 1)
Err Missing -> Ok 1
string = "Button A: X+94, Y+34"
(_, xyStr) = try bisect string ": "
(xStr, yStr) = try bisect xyStr ", "
This seems quite powerful for writing an off-the-cuff parser without nested pattern matching, in combination with Str.dropPrefix
and other things.
bisect is just a single split?
Also, upsert sounds like something we should add...I wonder if there is a better name.
Brendan Hansknecht said:
bisect is just a single split?
Yes, I originally called it splitTwo
:
splitTwo : Str, Str -> Result (Str, Str) [CouldNotSpli]
splitTwo = \str, on ->
when Str.splitOn str on is
[a, b] -> Ok (a, b)
_ -> Err CouldNotSplit
splitThree (trisect?) could also be useful as well
Being able to destructure the result is key I think
oh, bisect is exactly two, so not the same as splitting at the first instance of the substring (that is what I initially assumed it was)
Ah yeah. That may be more useful for real world stuff whereas my bisect
is only useful for parsing extremely constrained inputs.
I'm not sure there are any cases where I could not just have used Str.splitFirst
to be honest.
Last updated: Jul 06 2025 at 12:14 UTC