Array.purs 739 Bytes
Newer Older
1
module Gargantext.Utils.Array (max, min, push) where
James Laver's avatar
James Laver committed
2

3 4 5 6
import Data.Array as A
import Data.Foldable (foldr)
import Data.Maybe (Maybe(..))
import Data.Ord as Ord
James Laver's avatar
James Laver committed
7 8 9
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)

10 11
import Gargantext.Prelude

James Laver's avatar
James Laver committed
12 13 14 15
foreign import _push :: forall a. EffectFn2 (Array a) a Unit

push :: forall a. Array a -> a -> Effect Unit
push = runEffectFn2 _push
16 17 18 19 20 21 22 23 24 25 26 27 28


max :: forall a. Ord a => Array a -> Maybe a
max xs = foldr reducer (A.head xs) xs
  where
    reducer _ Nothing = Nothing
    reducer v (Just acc) = Just $ Ord.max acc v

min :: forall a. Ord a => Array a -> Maybe a
min xs = foldr reducer (A.head xs) xs
  where
    reducer _ Nothing = Nothing
    reducer v (Just acc) = Just $ Ord.min acc v