~jaro/balkon

ref: cf3ec3e35ebdbbe540c0a1490545622157271543 balkon/src/Data/Text/ParagraphLayout/Internal/SplitList.hs -rw-r--r-- 2.2 KiB
cf3ec3e3Jaro Allow trimmed whitespace to generate empty fragments. 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- | Utility functions to work with pairs of lists, which may be non-empty.
module Data.Text.ParagraphLayout.Internal.SplitList
    ( allowFstEmpty
    , allowSndEmpty
    , nonEmptyPairs
    , nonEmptyFsts
    , nonEmptySnds
    )
where

import Data.List.NonEmpty (NonEmpty, nonEmpty, toList)
import Data.Maybe (catMaybes)

-- | Generalise the first component of a pair from a non-empty list
-- to a regular list.
allowFstEmpty :: (NonEmpty a, b) -> ([a], b)
allowFstEmpty (a, b) = (toList a, b)

-- | Generalise the second component of a pair from a non-empty list
-- to a regular list.
allowSndEmpty :: (a, NonEmpty b) -> (a, [b])
allowSndEmpty (a, b) = (a, toList b)

-- | Turn pairs of normal lists into pairs of `NonEmpty` lists,
-- removing pairs in which either list is empty.
nonEmptyPairs :: [([a], [b])] -> [(NonEmpty a, NonEmpty b)]
nonEmptyPairs = catMaybes . map nonEmptyPair

-- | Turn a pair of normal lists into `Just` a pair of `NonEmpty` lists,
-- or `Nothing` if either list is empty.
nonEmptyPair :: ([a], [b]) -> Maybe (NonEmpty a, NonEmpty b)
nonEmptyPair (xs, ys) = case (nonEmpty xs, nonEmpty ys) of
    (Just xs1, Just ys1) -> Just (xs1, ys1)
    (_, _) -> Nothing

-- | Turn pairs of normal lists into pairs where the first list is `NonEmpty`,
-- removing pairs in which the first list is empty.
nonEmptyFsts :: [([a], [b])] -> [(NonEmpty a, [b])]
nonEmptyFsts = catMaybes . map nonEmptyFst

-- | Turn a pair of normal lists into `Just` a pair where the first list is
-- `NonEmpty`, or `Nothing` if the first list is empty.
nonEmptyFst :: ([a], [b]) -> Maybe (NonEmpty a, [b])
nonEmptyFst (xs, ys) = case nonEmpty xs of
    Just xs1 -> Just (xs1, ys)
    Nothing -> Nothing

-- | Turn pairs of normal lists into pairs where the second list is `NonEmpty`,
-- removing pairs in which the second list is empty.
nonEmptySnds :: [([a], [b])] -> [([a], NonEmpty b)]
nonEmptySnds = catMaybes . map nonEmptySnd

-- | Turn a pair of normal lists into `Just` a pair where the second list is
-- `NonEmpty`, or `Nothing` if the second list is empty.
nonEmptySnd :: ([a], [b]) -> Maybe ([a], NonEmpty b)
nonEmptySnd (xs, ys) = case nonEmpty ys of
    Just ys1 -> Just (xs, ys1)
    Nothing -> Nothing