Stream: beginners

Topic: No max, mix, clamp? 🤔


view this post on Zulip Fábio Beirão (Jun 09 2023 at 08:06):

I was a bit surprised to not find "common" functions such as max, min or clamp (like they exist in Elm Basics

I assume this is a conscious and deliberate decision. I tried to find a rationale on the tutorial, but there's no mention of min, max or clamp in there. Also tried looking for it in the different-names section, nothing. I am curious as to why these ""basic"" functions are missing from the Standard Library :thinking:

view this post on Zulip Hannes (Jun 09 2023 at 08:33):

Im sure you know this, but if you need the max of two numbers you can use List.max [a, b], I don't know if the compiler is (or is planned to be) smart enough to realise that's just a comparison between two numbers though.

view this post on Zulip Anton (Jun 09 2023 at 08:38):

I assume this is a conscious and deliberate decision.

I don't remember us discussing that, I'm in favor of adding them.

view this post on Zulip Hannes (Jun 09 2023 at 08:38):

Oh, and with List.max you'd then have to unwrap the Result because List.max doesn't know the list can't be empty, so there's a bit more friction to using it, so I think adding a Num.max function makes sense

view this post on Zulip Fábio Beirão (Jun 09 2023 at 08:38):

I understand, it's a possible workaround, but, to be honest it feels a bit "clunky".. also now the user would need to deal with Result (Num a) [ListWasEmpty] :confounded:
If that would be the idiomatic way to do it in Roc, then I would expect to find it either in the tutorial or in the standard documentation.

view this post on Zulip Richard Feldman (Jun 09 2023 at 10:33):

I think all three of those are fine to have as builtins! :thumbs_up:

view this post on Zulip Richard Feldman (Jun 09 2023 at 10:39):

if you'd like to add them, https://github.com/roc-lang/roc/commit/fb0a64dc228b9854567bcdfb36f84d6339e77250 is a nice minimal example of adding to the Num module

view this post on Zulip Fábio Beirão (Jun 09 2023 at 10:44):

I'm on it :smile:

view this post on Zulip Fábio Beirão (Jun 09 2023 at 11:14):

I think min and max have very simple signatures of Num a, Num a -> Num a
For clamp we could go two ways:

Way 1
clamp : Num a, Num a, Num a -> Num a
or
Way 2
clamp : Num a, { min: Num a, max: Num a } -> Num a

The usage for Way 1 would look like

myValue |> clamp 0 100
clamp myValue 0 100

The usage for Way 2 would look like

myValue |> clamp { min: 0, max: 100 }
clamp myValue { min: 0, max: 100 }

The inspiration for Way 2 comes from List.sublist
Do you guys have a particular preference?

If the clamp signature would need to be discussed further, I am okay with having two PRs, one for min/max and a subsequent one for clamp :+1:

view this post on Zulip Richard Feldman (Jun 09 2023 at 11:17):

yeah I'd say make one PR for min/max and then start a discussion in #ideas about clamp design!

view this post on Zulip Fábio Beirão (Jun 09 2023 at 12:20):

:point_up: Issue 5533 for the Min Max, and PR 5534

view this post on Zulip Fábio Beirão (Jun 09 2023 at 15:56):

:confounded: I need some help understanding what is going on with these tests

view this post on Zulip Anton (Jun 09 2023 at 16:01):

To fix this, you can run those tests locally, this will have changed a bunch of files, git add those, commit and push :)

view this post on Zulip Anton (Jun 09 2023 at 16:01):

There's a message in there that explains it, but it's easy to miss.

view this post on Zulip Fábio Beirão (Jun 09 2023 at 16:10):

Okay, added those files, hopefully 3rd time's the charm :sweat_smile:

view this post on Zulip Fábio Beirão (Jun 09 2023 at 19:02):

:point_up: Yay, the PR's tests are finally :check: Thanks Anton :pray:

view this post on Zulip Fábio Beirão (Jun 11 2023 at 13:41):

@Anton can I get the merge on PR 5534? :pray: Thank you!


Last updated: Jul 06 2025 at 12:14 UTC