by mauricio · 6y ago · 0 comments
25.clamp(5, 10)
=> 10
6.clamp(5, 10)
=> 6
1.clamp(5, 10)
=> 5https://repl.it/repls/GargantuanThistleLink
Result from sort: 3 in 0.9785124980007822s
Allocated 3000001 object(s)
Result from ternary: 3 in 0.3205206830025418s
Allocated 1 object(s)
Result from clamp: 3 in 0.5030354310001712s
Allocated 2 object(s)
Interestingly the ternary comparison is faster than clamp.squish(25, c(5, 10)) => 10
squish(6, c(5, 10)) => 6
squish(1, c(5, 10)) => 5
If you don't provide the limits it defaults to c(0, 1). That's because this function exists to map to a 0-to-1 range for functions that then map the [0, 1] range to a color ramp.