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:
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.
I assume this is a conscious and deliberate decision.
I don't remember us discussing that, I'm in favor of adding them.
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
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.
I think all three of those are fine to have as builtins! :thumbs_up:
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
I'm on it :smile:
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:
yeah I'd say make one PR for min/max and then start a discussion in #ideas about clamp
design!
:point_up: Issue 5533 for the Min Max, and PR 5534
:confounded: I need some help understanding what is going on with these tests
To fix this, you can run those tests locally, this will have changed a bunch of files, git add
those, commit and push :)
There's a message in there that explains it, but it's easy to miss.
Okay, added those files, hopefully 3rd time's the charm :sweat_smile:
:point_up: Yay, the PR's tests are finally :check: Thanks Anton :pray:
@Anton can I get the merge on PR 5534? :pray: Thank you!
Last updated: Jul 06 2025 at 12:14 UTC