Array.purs 1.05 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
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
14
import Math as Math
James Laver's avatar
James Laver committed
15

16 17
import Gargantext.Prelude

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

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


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

-- | 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
    end' = DI.round $ Math.floor $ (DI.toNumber $ end - start) / (DI.toNumber step)