def clamp(min, _max, n) when nmax, do: max
def clamp(_min, _max, n), do: nMath.min(Math.max(num, min), max)
31–40 of 291 posts
Re: Math.min(Math.max(num, min), max)
#32[min, num, max].sort()[1]
sort() on JavaScript arrays sorts alphabetically (unless you pass a compareFunction) https://stackoverflow.com/questions/21019902/why-cant-javasc... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re: Math.min(Math.max(num, min), max)
#33I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think about it. I find the following easier to read : Math.min(Math.max(num, lower_bound), upper_bound)
Re: Math.min(Math.max(num, min), max)
#34[min, num, max].sort()[1]
sort() on JavaScript arrays sorts alphabetically (unless you pass a compareFunction) https://stackoverflow.com/questions/21019902/why-cant-javasc... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
[min, num, max].sort((a, b) => Math.sign(a - b))[1]
An entirely different alternative is poor man's match: switch (true) {
case num > max: return max;
case num Re: Math.min(Math.max(num, min), max)
#35I use Common Lisp:
> (max min (min max n))
Is the difference my choice of language, my personal mental hardware, the amount of familiarity one has with the pattern, or none of the above?
Re: Math.min(Math.max(num, min), max)
#36Here's clamp in idiomatic Elixir (using multi-clause functions and guards): def clamp(min, _max, n) when n max, do: max def clamp(_min, _max, n), do: n
Maybe something like this?
defmodule Compare do
def clamp(number, minimum, maximum) do
number
|> max(minimum)
|> min(maximum)
end
end
import Compare
clamp(5, 1, 10) # 5
clamp(1, 5, 10) # 5
clamp(10, 1, 5) # 5
some_number
|> clamp(min, max)
As a side note, I think the Elixir |> operator is a stroke of genius that other languages should take a look at. Making the pipe operator append the _first_ argument has the following benefits1.) It makes the most "important" argument of the function the first thing you read in function signatures
2.) If you need to add more arguments to a function signature later, they tend to be less important the original args, so they tend to make sense at the end
3.) It creates a convention for all libraries to follow so they can leverage the pipe operator. Its really jarring when the thing you want to put in a pipeline isn't the first argument (looking at you `Regex`[0] which puts the regular expression as the first arg and not the string)
Re: Math.min(Math.max(num, min), max)
#37 xs: 1 2 3 4 5
max: 4
min: 2
max& min| xs
2 2 3 4 4
works for scalar, vector, matrixRe: Math.min(Math.max(num, min), max)
#38 func helper(a float64, c chan float64){
time.Sleep(time.Duration(a) * time.Second)
c Re: Math.min(Math.max(num, min), max)
#39 val - (sign(val - max) + 1) / 2 * (val - max) + (sign(min - val) + 1) / 2 * (min - val)Re: Math.min(Math.max(num, min), max)
#40Luckily, this is a solved problem for go. func helper(a float64, c chan float64){ time.Sleep(time.Duration(a) * time.Second) c