Skip to content

Commit e277097

Browse files
committed
Merge pull request #78 from hdgarrood/add-ord-ring-functions
Add abs, signum. resolves #66
2 parents a089c06 + e1a1d90 commit e277097

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Data/Ord.purs

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ module Data.Ord
88
, min, max
99
, clamp
1010
, between
11+
, abs
12+
, signum
1113
, module Data.Ordering
1214
) where
1315

1416
import Data.Eq (class Eq)
1517
import Data.Function (on)
1618
import Data.Ord.Unsafe (unsafeCompare)
1719
import Data.Ordering (Ordering(..))
18-
import Data.Ring (negate)
20+
import Data.Ring (class Ring, zero, one, negate)
1921
import Data.Unit (Unit)
2022
import Data.Void (Void)
2123

@@ -149,3 +151,13 @@ between low hi x
149151
| x < low = false
150152
| x > hi = false
151153
| true = true
154+
155+
-- | The absolute value function. `abs x` is defined as `if x >= zero then x
156+
-- | else negate x`.
157+
abs :: forall a. (Ord a, Ring a) => a -> a
158+
abs x = if x >= zero then x else negate x
159+
160+
-- | The sign function; always evaluates to either `one` or `negate one`. For
161+
-- | any `x`, we should have `signum x * abs x == x`.
162+
signum :: forall a. (Ord a, Ring a) => a -> a
163+
signum x = if x >= zero then one else negate one

0 commit comments

Comments
 (0)