Sequence.hs
1 module Utils.Sequence (firstValue, lastValue, withoutLast) where 2 3 import Data.Sequence as Seq (ViewL (EmptyL, (:<)), length, lookup, null, take, viewl) 4 import Text.Pandoc.Builder (Many (Many, unMany)) 5 6 -- Gets the first value of a sequence wrapped in Many 7 firstValue :: Many a -> Maybe a 8 firstValue many = case Seq.viewl xs of 9 Seq.EmptyL -> Nothing 10 x Seq.:< _ -> Just x 11 where 12 xs = unMany many 13 14 -- Gets the last value of a sequence wrapped in Many 15 lastValue :: Many a -> Maybe a 16 lastValue many = Seq.lookup (Seq.length extractedSeq - 1) extractedSeq 17 where 18 extractedSeq = unMany many 19 20 -- Return a sequence without the last element of the input sequence 21 withoutLast :: Many a -> Many a 22 withoutLast many 23 | Seq.null xs = many 24 | otherwise = Many $ Seq.take (Seq.length xs - 1) xs 25 where 26 xs = unMany many