Stream: beginners

Topic: AoC standard library


view this post on Zulip Alex Nuttall (Dec 13 2024 at 18:16):

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:

List sumBy

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?

Dict upsert

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 bisect

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.

view this post on Zulip Brendan Hansknecht (Dec 13 2024 at 18:21):

bisect is just a single split?

view this post on Zulip Brendan Hansknecht (Dec 13 2024 at 18:21):

Also, upsert sounds like something we should add...I wonder if there is a better name.

view this post on Zulip Alex Nuttall (Dec 13 2024 at 18:27):

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

view this post on Zulip Alex Nuttall (Dec 13 2024 at 18:27):

Being able to destructure the result is key I think

view this post on Zulip Brendan Hansknecht (Dec 13 2024 at 18:29):

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)

view this post on Zulip Alex Nuttall (Dec 13 2024 at 18:40):

Ah yeah. That may be more useful for real world stuff whereas my bisect is only useful for parsing extremely constrained inputs.

view this post on Zulip Alex Nuttall (Dec 13 2024 at 18:44):

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