Array.purs 1.02 KB
Newer Older
1 2 3 4 5
module Gargantext.Utils.Array (
    max
  , min
  , push
  , range) where
James Laver's avatar
James Laver committed
6

7 8
import Data.Array as A
import Data.Foldable (foldr)
9
import Data.Int as DI
10 11
import Data.Maybe (Maybe(..))
import Data.Ord as Ord
James Laver's avatar
James Laver committed
12 13 14
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)

15 16
import Gargantext.Prelude

James Laver's avatar
James Laver committed
17 18 19 20
foreign import _push :: forall a. EffectFn2 (Array a) a Unit

push :: forall a. Array a -> a -> Effect Unit
push = runEffectFn2 _push
21 22 23 24 25 26 27 28 29 30 31 32 33


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
34 35 36 37 38

-- | Create an array containing a range of integers, with given step
range :: Int -> Int -> Int -> Array Int
range start end step = map (\i -> start + i*step) $ A.range 0 end'
  where
39
    end' = DI.round $ DI.floor $ (DI.toNumber $ end - start) / (DI.toNumber step)