sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript

Home Page:https://sanctuary.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implementation of R.clamp

Fl4m3Ph03n1x opened this issue · comments

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 ....

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.

How does this implementation look?

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