Source file seq_e.ml
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
open Monad
type (+'a, 'e) node = Nil | Cons of 'a * ('a, 'e) t
and ('a, 'e) t = unit -> (('a, 'e) node, 'e) result
let nil = Nil
let nil_e = Ok Nil
let empty () = Ok Nil
let return x () = Ok (Cons (x, empty))
let return_e r () = Result.map (fun x -> Cons (x, empty)) r
let interrupted e () = Error e
let cons item t () = Ok (Cons (item, t))
let cons_e item t () = item >|? fun item -> Cons (item, t)
let rec append ta tb () =
ta () >>? function
| Nil -> tb ()
| Cons (item, ta) -> Ok (Cons (item, append ta tb))
let first s =
match s () with
| Ok Nil -> None
| Ok (Cons (x, _)) -> Some (Ok x)
| Error _ as error -> Some error
let rec fold_left f acc seq =
seq () >>? function
| Nil -> Ok acc
| Cons (item, seq) -> fold_left f (f acc item) seq
let rec fold_left_e f acc seq =
seq () >>? function
| Nil -> Ok acc
| Cons (item, seq) -> f acc item >>? fun acc -> fold_left_e f acc seq
let rec fold_left_s f acc seq =
seq () >>?= function
| Nil -> Monad.return acc
| Cons (item, seq) -> f acc item >>= fun acc -> fold_left_s f acc seq
let fold_left_s f acc seq =
seq () >>?= function
| Nil -> Monad.return acc
| Cons (item, seq) ->
lwt_apply2 f acc item >>= fun acc -> fold_left_s f acc seq
let rec fold_left_es f acc seq =
seq () >>?= function
| Nil -> Monad.return acc
| Cons (item, seq) -> f acc item >>=? fun acc -> fold_left_es f acc seq
let fold_left_es f acc seq =
seq () >>?= function
| Nil -> Monad.return acc
| Cons (item, seq) ->
lwt_apply2 f acc item >>=? fun acc -> fold_left_es f acc seq
let rec iter f seq =
seq () >>? function
| Nil -> unit_e
| Cons (item, seq) ->
f item ;
iter f seq
let rec iter_e f seq =
seq () >>? function
| Nil -> unit_e
| Cons (item, seq) -> f item >>? fun () -> iter_e f seq
let rec iter_s f seq =
seq () >>?= function
| Nil -> unit_es
| Cons (item, seq) -> f item >>= fun () -> iter_s f seq
let iter_s f seq =
seq () >>?= function
| Nil -> unit_es
| Cons (item, seq) -> Lwt.apply f item >>= fun () -> iter_s f seq
let rec iter_es f seq =
seq () >>?= function
| Nil -> unit_es
| Cons (item, seq) -> f item >>=? fun () -> iter_es f seq
let iter_es f seq =
seq () >>?= function
| Nil -> unit_es
| Cons (item, seq) -> Lwt.apply f item >>=? fun () -> iter_es f seq
let iter_p f seq =
let rec iter_p acc f seq =
match seq () with
| Error _ as e -> join_p acc >>= fun () -> Lwt.return e
| Ok Nil -> join_p acc >>= fun () -> Monad.unit_es
| Ok (Cons (item, seq)) -> iter_p (Lwt.apply f item :: acc) f seq
in
iter_p [] f seq
let rec map f seq () =
seq () >|? function Nil -> Nil | Cons (item, seq) -> Cons (f item, map f seq)
let rec map_e f seq () =
seq () >>? function
| Nil -> nil_e
| Cons (item, seq) -> f item >>? fun item -> Ok (Cons (item, map_e f seq))
let rec map_error (f : 'e -> 'f) (seq : ('a, 'e) t) : ('a, 'f) t =
fun () ->
match seq () with
| Ok Nil -> Ok Nil
| Ok (Cons (item, seq)) -> Ok (Cons (item, map_error f seq))
| Error e -> Error (f e)
let rec filter f seq () =
seq () >>? function
| Nil -> nil_e
| Cons (item, seq) ->
if f item then Ok (Cons (item, seq)) else filter f seq ()
let rec filter_e f seq () =
seq () >>? function
| Nil -> nil_e
| Cons (item, seq) -> (
f item >>? function
| true -> Ok (Cons (item, filter_e f seq))
| false -> filter_e f seq ())
let rec filter_map f seq () =
seq () >>? function
| Nil -> nil_e
| Cons (item, seq) -> (
match f item with
| None -> filter_map f seq ()
| Some item -> Ok (Cons (item, filter_map f seq)))
let rec filter_map_e f seq () =
seq () >>? function
| Nil -> nil_e
| Cons (item, seq) -> (
f item >>? function
| None -> filter_map_e f seq ()
| Some item -> Ok (Cons (item, filter_map_e f seq)))
let rec unfold f a () =
match f a with
| None -> nil_e
| Some (item, a) -> Ok (Cons (item, unfold f a))
let rec unfold_e f a () =
f a >>? function
| None -> nil_e
| Some (item, a) -> Ok (Cons (item, unfold_e f a))
let rec of_seq seq () =
match seq () with
| Stdlib.Seq.Nil -> nil_e
| Stdlib.Seq.Cons (e, seq) -> Ok (Cons (e, of_seq seq))
let rec of_seq_e seq () =
match seq () with
| Stdlib.Seq.Nil -> nil_e
| Stdlib.Seq.Cons (Ok e, seq) -> Ok (Cons (e, of_seq_e seq))
| Stdlib.Seq.Cons ((Error _ as e), _) -> e