Seq.purs 601 Bytes
Newer Older
1
module Gargantext.Utils.Seq (groupBy, mapMaybe) where
2

3
import Data.Array ((:))
4
import Data.Maybe (Maybe, maybe)
5 6
import Data.Sequence (Seq, concatMap, empty, null, singleton, splitAt)
import Data.Tuple (Tuple(..))
7

8
import Gargantext.Prelude ((<<<))
9 10

mapMaybe :: forall a b. (a -> Maybe b) -> Seq a -> Seq b
11
mapMaybe f = concatMap (maybe empty singleton <<< f)
12 13 14 15 16 17 18 19 20

-- | Group a given `Seq` into list of `Seq`'s of length `n` (last one can be shorter)
groupBy :: forall a. Int -> Seq a -> Array (Seq a)
groupBy n s =
  if null s
  then []
  else
    let (Tuple hd tl) = splitAt n s in
    (hd:groupBy n tl)