Skip to content

Commit 59b33b3

Browse files
committed
Merge pull request #36 from hdgarrood/maximum-minimum
Add maximum and minimum
2 parents 544e318 + 193e90f commit 59b33b3

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
},
2929
"devDependencies": {
3030
"purescript-console": "^0.1.0",
31-
"purescript-assert": "^0.1.0"
31+
"purescript-assert": "^0.1.0",
32+
"purescript-functions": "~0.1.0",
33+
"purescript-integers": "~0.2.1",
34+
"purescript-math": "~0.2.0"
3235
}
3336
}

src/Data/Foldable.purs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module Data.Foldable
1616
, elem
1717
, notElem
1818
, find
19+
, maximum
20+
, maximumBy
21+
, minimum
22+
, minimumBy
1923
) where
2024

2125
import Prelude
@@ -228,3 +232,33 @@ notElem x = not <<< elem x
228232
-- | Try to find an element in a data structure which satisfies a predicate.
229233
find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
230234
find p = foldl (\r x -> if p x then Just x else r) Nothing
235+
236+
-- | Find the largest element of a structure, according to its `Ord` instance.
237+
maximum :: forall a f. (Ord a, Foldable f) => f a -> Maybe a
238+
maximum = maximumBy compare
239+
240+
-- | Find the largest element of a structure, according to a given comparison
241+
-- | function. The comparison function should represent a total ordering (see
242+
-- | the `Ord` type class laws); if it does not, the behaviour is undefined.
243+
maximumBy :: forall a f. (Foldable f) => (a -> a -> Ordering) -> f a -> Maybe a
244+
maximumBy cmp = foldl max' Nothing
245+
where
246+
max' Nothing x = Just x
247+
max' (Just x) y = Just (case cmp x y of
248+
GT -> x
249+
_ -> y)
250+
251+
-- | Find the smallest element of a structure, according to its `Ord` instance.
252+
minimum :: forall a f. (Ord a, Foldable f) => f a -> Maybe a
253+
minimum = minimumBy compare
254+
255+
-- | Find the smallest element of a structure, according to a given comparison
256+
-- | function. The comparison function should represent a total ordering (see
257+
-- | the `Ord` type class laws); if it does not, the behaviour is undefined.
258+
minimumBy :: forall a f. (Foldable f) => (a -> a -> Ordering) -> f a -> Maybe a
259+
minimumBy cmp = foldl min' Nothing
260+
where
261+
min' Nothing x = Just x
262+
min' (Just x) y = Just (case cmp x y of
263+
LT -> x
264+
_ -> y)

test/Main.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import Prelude
44

55
import Control.Monad.Eff (Eff())
66
import Control.Monad.Eff.Console
7+
import Data.Int (toNumber)
8+
import Data.Function (on)
9+
import Math (abs)
710
import Data.Maybe
811
import Data.Monoid.Additive
912
import Data.Foldable
@@ -70,6 +73,24 @@ main = do
7073
log "Test bisequenceDefault"
7174
testBitraversableIOrWith BSD
7275

76+
log "Test maximum"
77+
assert $ maximum (arrayFrom1UpTo 10) == Just 10
78+
79+
log "Test maximumBy"
80+
assert $
81+
maximumBy (compare `on` abs)
82+
(map (negate <<< toNumber) (arrayFrom1UpTo 10))
83+
== Just (-10.0)
84+
85+
log "Test minimum"
86+
assert $ minimum (arrayFrom1UpTo 10) == Just 1
87+
88+
log "Test minimumBy"
89+
assert $
90+
minimumBy (compare `on` abs)
91+
(map (negate <<< toNumber) (arrayFrom1UpTo 10))
92+
== Just (-1.0)
93+
7394
log "All done!"
7495

7596

0 commit comments

Comments
 (0)