sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

Implementation of R.clamp #574

Closed Fl4m3Ph03n1x closed 5 years ago

Fl4m3Ph03n1x commented 6 years ago

So, the name speaks for itself. I vote we should implement R.clamp and I agree with the interface given by @davidchambers

S.clamp :: Ord a => a -> a -> a -> a

This means we would not only be able to clamp Numbers, but also Strings and pretty much anything that has an order.

As for implementation ... I would have no idea on how to implement it ....

Bradcomp commented 6 years ago

You could probably start by looking at the Ramda implementation and then going from there. It should be pretty similar, but swap out gt and lt for the > and < operators.

davidchambers commented 6 years ago

How does this implementation look?

//    clamp :: Ord a => a -> a -> a -> a
const clamp = lower => upper => S.compose (S.min (upper)) (S.max (lower));