module Data.Ord.HT where

import Data.Function.HT (compose2, )

{-# INLINE comparing #-}
comparing :: Ord b => (a -> b) -> a -> a -> Ordering
comparing :: forall b a. Ord b => (a -> b) -> a -> a -> Ordering
comparing = (b -> b -> Ordering) -> (a -> b) -> a -> a -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
compose2 b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare

{- |
@limit (lower,upper) x@ restricts @x@ to the range from @lower@ to @upper@.
Don't expect a sensible result for @lower>upper@.

Called @clamp@ elsewhere.
-}
{-# INLINE limit #-}
limit :: (Ord a) => (a,a) -> a -> a
limit :: forall a. Ord a => (a, a) -> a -> a
limit (a
l,a
u) = a -> a -> a
forall a. Ord a => a -> a -> a
max a
l (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> a
forall a. Ord a => a -> a -> a
min a
u

{- |
@limit (lower,upper) x@ checks whether @x@ is in the range from @lower@ to @upper@.
Don't expect a sensible result for @lower>upper@.
-}
{-# INLINE inRange #-}
inRange :: (Ord a) => (a,a) -> a -> Bool
inRange :: forall a. Ord a => (a, a) -> a -> Bool
inRange (a
l,a
u) a
x  =  a
la -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
x Bool -> Bool -> Bool
&& a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
u