-
Notifications
You must be signed in to change notification settings - Fork 1
/
StdIter.fs
240 lines (188 loc) · 6.79 KB
/
StdIter.fs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
module rec Std.StdIter
type private NextFun<'S, 'T> = 'S -> ('T * 'S) option
// The implementation of the Iter type.
//
// If generic nominal type were implemented,
// iterator (enumerator in .NET) would be defined as:
// Iter<'T> = Iter of unit -> ('T * Iter<'T>) option
//
// The recursion of type can be avoided by
// splitting the function and the state that changes during iteration.
// That is:
// Iter<'T> = (forall 'S => 'S -> ('T * 'S) option)
//
// `forall` is not supported (no plan to support).
// 'S is just replaced by obj here.
/// Iterator, abstraction of iteration.
///
/// Also known as "enumerator" in .NET.
/// Iterator is stateless and different than IEnumerator.
type Iter<'T> = NextFun<obj, 'T> * obj
module Iter =
let next (xs: Iter<'T>) : ('T * Iter<'T>) option =
let nextFun, state = xs
match nextFun state with
| None -> None
| Some (item, state) ->
let xs: Iter<'T> = nextFun, state
Some(item, xs)
/// Extended `scan`.
let private scanEx (f: 'S -> 'T -> Iter<'U> * 'S) (state: 'S) (xs: Iter<'T>) : Iter<'U> =
let innerNextFun, innerState = xs
let pack (subIterOpt: Iter<'U> option) (innerState: obj) (state: 'S) = box (subIterOpt, innerState, state)
let unpack (outerState: obj) : Iter<'U> option * obj * 'S = unbox outerState
let rec scanExLoop
(ysOpt: Iter<'U> option)
(innerState: obj)
(state: 'S)
: ('U * (Iter<'U> option * obj * 'S)) option =
match ysOpt with
| Some ys ->
match next ys with
| None -> scanExLoop None innerState state
| Some (y, ys) -> Some(y, (Some ys, innerState, state))
| None ->
match innerNextFun innerState with
| None -> None
| Some (x, innerState) ->
let ys, state = f state x
scanExLoop (Some ys) innerState state
let nextFun outerState : ('U * obj) option =
let ysOpt, innerState, state = unpack outerState
match scanExLoop ysOpt innerState state with
| None -> None
| Some (y, (ysOpt, innerState, state)) -> Some(y, pack ysOpt innerState state)
nextFun, pack None innerState state
let map (f: 'T -> 'U) (xs: Iter<'T>) : Iter<'U> =
let innerNextFun, state = xs
let nextFun (state: obj) : ('U * obj) option =
match innerNextFun state with
| None -> None
| Some (x, state) -> Some(f x, state)
nextFun, state
// ---------------------------------------------
// Constructor
// ---------------------------------------------
let empty () : Iter<'T> =
let nextFun (_: obj) : ('T * obj) option = None
nextFun, box ()
let singleton (item: 'T) : Iter<'T> =
let nextFun (state: obj) : ('T * obj) option =
if unbox state = 0 then
Some(item, box 1)
else
None
nextFun, box 0
let range (start: int) (endIndex: int) : Iter<int> =
let nextFun state =
let i = unbox state
if i < endIndex then
Some(i, box (i + 1))
else
None
nextFun, box start
let ofOption (opt: 'T option) : Iter<'T> =
match opt with
| Some item -> singleton item
| None -> empty ()
let ofList (xs: 'T list) : Iter<'T> =
let nextFun (state: obj) : ('T * obj) option =
match unbox state with
| [] -> None
| x :: xs -> Some(x, box xs)
nextFun, box xs
let init (count: int) (initializer: int -> 'T) : Iter<'T> = range 0 count |> map initializer
let replicate (count: int) (item: 'T) : Iter<'T> = range 0 count |> map (fun _ -> item)
let unfold (unfolder: 'S -> ('T * 'S) option) (state: 'S) : Iter<'T> =
let nextFun (state: obj) : ('T * obj) option =
match unfolder (unbox state) with
| None -> None
| Some (item, state) -> Some(item, box state)
nextFun, box state
// ---------------------------------------------
// Transformer
// ---------------------------------------------
let choose (f: 'T -> 'U option) (xs: Iter<'T>) : Iter<'U> =
let innerNextFun, state = xs
let rec nextFun (state: obj) : ('U * obj) option =
match innerNextFun state with
| None -> None
| Some (x, innerState) ->
match f x with
| None -> nextFun innerState
| Some y -> Some(y, innerState)
nextFun, state
let collect (f: 'T -> Iter<'U>) (xs: Iter<'T>) : Iter<'U> =
scanEx (fun () (x: 'T) -> f x, ()) () xs
let scan (folder: 'S -> 'T -> 'S) (state: 'S) (xs: Iter<'T>) : Iter<'S> =
scanEx
(fun state x ->
let state = folder state x
singleton state, state)
state
xs
let filter (pred: 'T -> bool) (xs: Iter<'T>) : Iter<'T> =
xs
|> choose (fun x -> if pred x then Some x else None)
let mapi (f: int -> 'T -> 'U) (xs: Iter<'T>) : Iter<'U> =
scanEx (fun (i: int) (x: 'T) -> singleton (f i x), i + 1) 0 xs
let indexed (xs: Iter<'T>) : Iter<int * 'T> =
scanEx (fun (i: int) (x: 'T) -> singleton (i, x), i + 1) 0 xs
let append (first: Iter<'T>) (second: Iter<'T>) : Iter<'T> =
scanEx
(fun () (i: int) ->
match i with
| 0 -> first, ()
| _ -> second, ())
()
(range 0 2)
// ---------------------------------------------
// Consumer
// ---------------------------------------------
let isEmpty (xs: Iter<_>) : bool = Option.isNone (next xs)
// wants Result<'S, 'A>, use 'S option * 'A option instead
/// Runs an iterator updating a state.
///
/// `folder state item` computes the next state `Ok s`
/// or returns `Err a` to break from the iteration.
let private foldEx (folder: 'S -> 'T -> 'S option * 'A option) (complete: 'S -> 'A) (state: 'S) (xs: Iter<'T>) : 'A =
let nextFun, iterState = xs
let rec foldExLoop (state: 'S) (iterState: obj) : 'A =
match nextFun iterState with
| None -> complete state
| Some (item, iterState) ->
match folder state item with
| Some state, None -> foldExLoop state iterState
| None, Some result -> result
| _ ->
assert false
exit 1
foldExLoop state iterState
let fold (folder: 'S -> 'T -> 'S) (state: 'S) (xs: Iter<'T>) : 'S =
foldEx (fun state item -> Some(folder state item), None) id state xs
let forall (pred: 'T -> bool) (xs: Iter<'T>) : bool =
foldEx
(fun _ x ->
if pred x then
Some(), None // Ok ()
else
None, Some false) // Err false
(fun () -> true)
()
xs
let exists (pred: 'T -> bool) (xs: Iter<'T>) : bool =
foldEx
(fun _ x ->
if pred x then
None, Some true // Err true
else
Some(), None) // Ok ()
(fun () -> false)
()
xs
let tryPick (picker: 'T -> 'U option) (xs: Iter<'T>) : 'U option =
match next (choose picker xs) with
| Some (it, _) -> Some it
| _ -> None
let toList (xs: Iter<'T>) : 'T list =
xs |> fold (fun xs x -> x :: xs) [] |> List.rev