-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathExtra.hs
More file actions
69 lines (59 loc) · 1.87 KB
/
Copy pathExtra.hs
File metadata and controls
69 lines (59 loc) · 1.87 KB
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
59
60
61
62
63
64
65
66
67
68
69
module Data.Either.Extra (collectErrors, delimit) where
import Prelude ()
import Relude
import qualified Data.ReversedList as ReversedList
import qualified Data.List as List
collectErrors :: [Either l r] -> Either [l] [r]
collectErrors list =
let
step acc next =
case (next, acc) of
(Left l, Right _) ->
Left [l]
(Left l, Left ls) ->
Left (l : ls)
(Right r, Right rs) ->
Right (r : rs)
(Right _, Left ls) ->
Left ls
in
foldl' step (Right []) list
{-| Could possibly be replaced by <https://hackage.haskell.org/package/utility-ht-0.0.16/docs/Data-List-HT.html#v:segmentBeforeRight>
-}
delimit :: [Either delim a] -> ([a], [ (delim, [a]) ])
delimit =
let
init =
( ReversedList.empty
, Left ()
)
step (cur, state) (Right b) =
( ReversedList.push b cur
, state
)
step (cur, state) (Left delim) =
( ReversedList.empty
, case state of
Left () ->
Right
( delim
, ReversedList.empty
, ReversedList.toList cur
)
Right (prev, secs, sec1) ->
Right
( delim
, ReversedList.push (prev,ReversedList.toList cur) secs
, sec1
)
)
done (cur, Left ()) =
( ReversedList.toList cur
, []
)
done (cur, Right (delim, secs, sec1)) =
( sec1
, ReversedList.toList $ ReversedList.push (delim, ReversedList.toList cur) secs
)
in
done . List.foldl' step init