Array.purs 1.63 KB
Newer Older
1 2 3 4
module Gargantext.Data.Array
  where

import Data.Array as DA
5
import Data.Maybe
6
import Data.Sequence as Seq
7 8
import Data.Tuple (Tuple(..))
import Prelude (bind, flip, identity, (<<<))
9

10 11
----------------------------------------------------------------------
-- | Split arrays tools
12 13 14 15 16 17 18
splitEvery :: forall a. Int -> Array a -> Array (Array a)
splitEvery _ [] = []
splitEvery n xs =
  let (Tuple h t) = splitAt n xs
  in DA.cons h (splitEvery n t)

splitAt :: forall a. Int -> Array a -> Tuple (Array a) (Array a)
19
splitAt n ls = Tuple (Seq.toUnfoldable x) (Seq.toUnfoldable xs)
20
  where
21
    Tuple x xs = Seq.splitAt n (Seq.fromFoldable ls)
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
----------------------------------------------------------------------
-- | Array with Maybe tools
mapMaybe :: forall a b. (a -> Maybe b) -> Array a -> Array b
mapMaybe f = concatMap (maybe [] singleton <<< f)

catMaybes :: forall a. Array (Maybe a) -> Array a
catMaybes = mapMaybe identity

----------------------------------------------------------------------
-- | Array misc tools
concatMap :: forall a b. (a -> Array b) -> Array a -> Array b
concatMap = flip bind

singleton :: forall a. a -> Array a
singleton a = [a]

39 40 41 42 43 44 45
----------------------------------------------------------------------
-- | Seq with Maybe tools
seqMapMaybe :: forall a b. (a -> Maybe b) -> Seq.Seq a -> Seq.Seq b
seqMapMaybe f = seqConcatMap (maybe Seq.empty Seq.singleton <<< f)

seqCatMaybes :: forall a. Seq.Seq (Maybe a) -> Seq.Seq a
seqCatMaybes = seqMapMaybe identity
46

47 48 49 50
----------------------------------------------------------------------
-- | Seq misc tools
seqConcatMap :: forall a b. (a -> Seq.Seq b) -> Seq.Seq a -> Seq.Seq b
seqConcatMap = flip bind