Closed
Description
How about some additional functions? Some are things I've used in the past, some are others I thought were sort of interesting, but might not be useful. Feel free to reject all of them.
Eq stuff
equaling :: forall b. (Eq b) => (a -> b) -> a -> a -> Boolean
equaling = on eq
Useful for things like intersectBy
or nubBy
.
Could also use a newtype to change the value of equality.
newtype Diff a = Diff a
instance (Eq a) => Eq (Diff a) where
(==) (Diff x) (Diff y) = x /= y
Ord stuff
ascending :: forall a b. (Ord b) => (a -> b) -> a -> a -> Ordering
ascending = on compare
clamp :: forall a. (Ord a) => a -> a -> a -> a
clamp low high x = max low (min high x)
clamped :: forall a. (Ord a) => a -> a -> a -> Boolean
clamped low high x = low <= x && x <= high
descending :: forall a b. (Ord b) => (a -> b) -> a -> a -> Ordering
descending = on (flip compare)
max :: forall a. (Ord a) => a -> a -> a
max x y = if x <= y then y else x
min :: forall a. (Ord a) => a -> a -> a
min x y = if x <= y then x else y
ascending
and descending
are helpful for when you use something like sortBy
. Also could use a newtype for changing the order of things, ala haskell:
newtype Desc a = Desc a
instance eqDesc :: (Eq a) => Eq (Desc a) where
(==) (Desc x) (Desc y) = x == y
instance ordDesc :: (Ord a) => Ord (Desc a) where
compare (Desc x) (Desc y) = compare y x
Semiring stuff
inc :: forall a. (Semiring a) => a -> a
inc = add one
Ring stuff
abs :: forall a. (Ord a, Ring a) => a -> a
abs x = if x <= zero then negate x else x
dec :: forall a. (Ring a) => a -> a
dec = flip sub one
sigNum :: forall a. (Ord a, ModuloSemiring a) => a -> a
sigNum x
| x == zero = zero
| otherwise = x `div` abs x
Metadata
Metadata
Assignees
Labels
No labels