package streaming

  1. Overview
  2. Docs

Source file Sink.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
open Types
open Utils

type ('a, 'b) t = ('a, 'b) sink =
  Sink : {
    init : unit -> 'acc;
    push : 'acc -> 'a -> 'acc;
    full : 'acc -> bool;
    stop : 'acc -> 'b;
  } -> ('a, 'b) t



let make ~init ~push ?(full=fun _ -> false) ~stop () =
  Sink { init; push; full; stop }


let fill b =
  Sink {
    init = (fun () -> ());
    push = (fun () _ -> ());
    full = (fun () -> true);
    stop = (fun () -> b)
  }


let map f (Sink k) =
  Sink { k with stop = (fun x -> f (k.stop x)) }


let (<@>) = map


let premap f (Sink k) =
  Sink { k with push = (fun acc x -> k.push acc (f x)) }


let prefilter f (Sink k) =
  Sink { k with push = (fun acc x -> if f x then k.push acc x else acc ) }


let zip (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) x = (l.push l_acc x, r.push r_acc x) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) = (l.stop l_acc, r.stop r_acc) in
  Sink { init; push; full; stop }


let zip_left (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) x = (l.push l_acc x, r.push r_acc x) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let l = l.stop l_acc in
    let _r = r.stop r_acc in
    l in
  Sink { init; push; full; stop }


let zip_right (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) x = (l.push l_acc x, r.push r_acc x) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let _l = l.stop l_acc in
    let r = r.stop r_acc in
    r in
  Sink { init; push; full; stop }


let zip_with f (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) x = (l.push l_acc x, r.push r_acc x) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let l = l.stop l_acc in
    let r = r.stop r_acc in
    f l r in
  Sink { init; push; full; stop }

let (<&>) = zip
let (<& ) = zip_left
let ( &>) = zip_right


let unzip (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) (x, y) = (l.push l_acc x, r.push r_acc y) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) = (l.stop l_acc, r.stop r_acc) in
  Sink { init; push; full; stop }

let unzip_left (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) (x, y) = (l.push l_acc x, r.push r_acc y) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let l = l.stop l_acc in
    let _r = r.stop r_acc in
    l in
  Sink { init; push; full; stop }


let unzip_right (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) (x, y) = (l.push l_acc x, r.push r_acc y) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let _l = l.stop l_acc in
    let r = r.stop r_acc in
    r in
  Sink { init; push; full; stop }


let unzip_with f (Sink l) (Sink r) =
  let init () = (l.init (), r.init ()) in
  let push (l_acc, r_acc) (x, y) = (l.push l_acc x, r.push r_acc y) in
  let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc) =
    let l = l.stop l_acc in
    let r = r.stop r_acc in
    f l r in
  Sink { init; push; full; stop }


let (<*>) = unzip
let (<* ) = unzip_left
let ( *>) = unzip_right

let distribute (Sink l) (Sink r) =
  let init () = (l.init (), r.init (), true) in
  let push (l_acc, r_acc, flag) x =
    if flag then (l.push l_acc x, r_acc, not flag)
    else (l_acc, r.push r_acc x, not flag) in
  let full (l_acc, r_acc, _) = (l.full l_acc || r.full r_acc) in
  let stop (l_acc, r_acc, _) = (l.stop l_acc, r.stop r_acc) in
  Sink { init; push; full; stop }


type ('a, 'b) race =
  | Left of 'a
  | Right of 'b
  | Both of 'a * 'b

let race (Sink l) (Sink r) =
  let init () = Both (l.init (), r.init ()) in
  let push state x =
    match state with
    | Both (l_acc, r_acc) ->
      let l_acc' = l.push l_acc x in
      let r_acc' = r.push r_acc x in
      if l.full l_acc' then Left  l_acc' else
      if r.full r_acc' then Right r_acc' else
        Both (l_acc', r_acc')
    | _ ->
        invalid_arg "Streaming.Sink.race: one of the sinks is already filled" in
  let full = function Both _ -> false | _ -> true in
  let stop = function
    | Left  l_acc -> Left (l.stop l_acc)
    | Right r_acc -> Right (r.stop r_acc)
    | Both (l_acc, r_acc) -> Both (l.stop l_acc, r.stop r_acc)
  in
  Sink { init; push; full; stop }

let (<|>) = race


let seq (Sink l) (Sink r) =
  let init () = `L (l.init ()) in
  let push state x =
    match state with
    | `L l_acc ->
      let l_acc' = l.push l_acc x in
      if l.full l_acc'
      then `R (l.stop l_acc', r.init ())
      else `L l_acc
    | `R (l_r, r_acc) -> `R (l_r, r.push r_acc x) in
  let full = function
    | `L l_acc -> l.full l_acc
    | `R (_, r_acc) -> r.full r_acc in
  let stop = function
    | `L l_acc ->
      let l_r = l.stop l_acc in
      let r_r = r.stop (r.init ()) in
      (l_r, r_r)
    | `R (l_r, r_acc) ->
      let r_r = r.stop r_acc in
      (l_r, r_r) in
  Sink { init; push; full; stop }


let seq_left (Sink l) (Sink r) =
  let init () = `L (l.init ()) in
  let push state x =
    match state with
    | `L l_acc ->
      let l_acc' = l.push l_acc x in
      if l.full l_acc'
      then `R (l.stop l_acc', r.init ())
      else `L l_acc
    | `R (l_r, r_acc) -> `R (l_r, r.push r_acc x) in
  let full = function
    | `L l_acc -> l.full l_acc
    | `R (_, r_acc) -> r.full r_acc in
  let stop = function
    | `L l_acc -> l.stop l_acc
    | `R (l_r, r_acc) ->
      let _r_r = r.stop r_acc in
      l_r in
  Sink { init; push; full; stop }


let seq_right (Sink l) (Sink r) =
  let init () = `L (l.init ()) in
  let push state x =
    match state with
    | `L l_acc ->
      let l_acc' = l.push l_acc x in
      if l.full l_acc'
      then (ignore (l.stop l_acc'); `R (r.init ()))
      else `L l_acc
    | `R r_acc -> `R (r.push r_acc x) in
  let full = function
    | `L l_acc -> l.full l_acc
    | `R r_acc -> r.full r_acc in
  let stop = function
    | `L l_acc ->
      ignore (l.stop l_acc);
      r.stop (r.init ())
    | `R r_acc -> r.stop r_acc in
  Sink { init; push; full; stop }

let (<+>) = seq
let (<+ ) = seq_left
let ( +>) = seq_right


type ('top, 'a, 'b) flat_map =
  | Flat_map_top : 'top -> ('top, 'a, 'b) flat_map
  | Flat_map_sub : {
    init: 'sub;
    push: 'sub -> 'a -> 'sub;
    full: 'sub -> bool;
    stop: 'sub -> 'b;
  } -> ('top, 'a, 'b) flat_map


let _flat_map f (Sink top) =
  let init () = Flat_map_top (top.init ()) in
  let push s x =
    match s with
    | Flat_map_top acc ->
      let acc' = top.push acc x in
      if top.full acc' then
        let r = top.stop acc' in
        let Sink sub = f r in
        Flat_map_sub {
          init = sub.init ();
          push = sub.push;
          full = sub.full;
          stop = sub.stop;
        }
      else
        Flat_map_top acc'
    | Flat_map_sub sub -> Flat_map_sub { sub with init = sub.push sub.init x } in
  let full = function
    | Flat_map_top acc -> top.full acc
    | Flat_map_sub sub -> sub.full sub.init in
  let stop = function
    | Flat_map_top acc -> top.stop acc (* XXX: This constrains the type to 'a. *)
    | Flat_map_sub sub -> sub.stop sub.init in
  Sink { init; push; full; stop }



let fold f z =
  Sink {
    init = (fun () -> z);
    push = f;
    full = (fun _ -> false);
    stop = (fun r -> r);
  }

let fold_while full f z =
  Sink {
    init = (fun () -> z);
    push = f;
    full = full;
    stop = (fun r -> r);
  }


let full =
  Sink {
    init = (fun () -> ());
    push = (fun () _ -> ());
    full = (fun () -> true);
    stop = (fun () -> ());
  }

let drain =
  Sink {
    init = (fun () -> ());
    push = (fun () _ -> ());
    full = (fun () -> false);
    stop = (fun () -> ());
  }

let each f =
  Sink {
    init = (fun () -> ());
    push = (fun () x -> f x);
    full = (fun () -> false);
    stop = (fun () -> ());
  }

let len =
  Sink {
    init = (fun () -> 0);
    push = (fun acc _ -> acc + 1);
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }

let mean =
  let init () = (0.0, 0.0) in
  let push (r, n) x =
     let n' = n +. 1.0 in
    ((r +. (x -. r) /. n'), n') in
  let stop (r, _) = r in
  let full _ = false in
  Sink { init; push; full; stop }



let nth_pure n =
  let init () = `Searching 0 in
  let push s x =
    match s with
    | `Searching i when eq_int i n -> `Found x
    | `Searching i -> `Searching (i + 1)
    | `Found x -> `Found x in
  let full s =
    match s with
    | `Searching _  -> false
    | `Found _ -> true in
  let stop s =
    match s with
    | `Searching _ -> None
    | `Found x -> Some x in
  Sink { init; push; full; stop }


let nth n =
  let r = ref (Obj.magic 0) in
  let init () = 0 in
  let push s x =
    if s = n then r := x;
    s + 1 in
  let full s = s > n in
  let stop s = if full s then Some !r else None in
  Sink { init; push; full; stop }


let first =
  Sink {
    init = (fun () -> None);
    push = (fun _ x -> Some x);
    full = (function None -> false | _ -> true);
    stop = (fun acc -> acc);
  }

let last =
  Sink {
    init = (fun () -> None);
    push = (fun _ x -> Some x);
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }


(* Finding elements *)

let contains ~where:pred =
  Sink {
    init = (fun () -> false);
    push = (fun found x -> if pred x then true else found);
    full = (fun found -> found);
    stop = (fun found -> found);
  }

let find ~where:pred =
  Sink {
    init = (fun () -> None);
    push = (fun none x -> if pred x then Some x else none);
    full = (function None -> false | _ -> true);
    stop = (fun acc -> acc);
  }

let index ~where:pred =
  let i = ref 0 in
  Sink {
    init = (fun () -> false);
    push = (fun acc x -> if pred x then true else (incr i; acc));
    full = (fun found -> found);
    stop = (fun found -> if found then Some !i else None);
  }


let maximum ~by:(>) =
  let push state x =
    match state with
    | Some y when y > x -> Some y
    | _ -> Some x in
  Sink {
    init = (fun () -> None);
    push;
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }


let minimum ~by:(<) =
  let push state x =
    match state with
    | Some y when y < x -> Some y
    | _ -> Some x in
  Sink {
    init = (fun () -> None);
    push;
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }


let is_empty =
  Sink {
    init = (fun () -> true);
    push = (fun _ _ -> false);
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }

let all ~where:pred =
  Sink {
    init = (fun () -> true);
    push = (fun acc x -> pred x && acc);
    full = (fun acc -> not acc);
    stop = (fun acc -> acc);
  }


let any ~where:pred =
  Sink {
    init = (fun () -> false);
    push = (fun acc x -> pred x || acc);
    full = (fun acc -> acc);
    stop = (fun acc -> acc);
  }


let print =
  each print_endline


let rev_list =
  Sink {
    init = (fun () -> []);
    push = (fun acc x -> x :: acc);
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }


let list =
  Sink {
    init = (fun () -> []);
    push = (fun acc x -> x :: acc);
    full = (fun _ -> false);
    stop = (fun acc -> List.rev acc);
  }


let array =
  let init () = ([], 0) in
  let push (acc, len) x = (x :: acc, len + 1) in
  let full _ = false in
  let stop (acc, len) =
    match acc with
    | [] -> [||] | [x] -> [|x|]
    | x :: xs ->
      let arr = Array.make len x in
      let rec loop i = function
        | [] -> arr
        | x :: xs ->
          Array.unsafe_set arr i x;
          loop (i - 1) xs in
      loop (len - 2) xs in
  Sink { init; push; full; stop }


let buffer n =
  if n < 0 then
    invalid_arg "Streaming.Sink.buffer: negative buffer size";
  if n = 0 then fill [||] else
  let buf = ref (Array.make n (Obj.magic 0)) in
  let init () = 0 in
  let push idx x =
    Array.set !buf idx x;
    idx + 1 in
  let full idx = (idx = n) in
  let stop idx =
    if idx < n then
      Array.sub !buf 0 idx
    else !buf in
  Sink { init; push; full; stop }



let queue =
  Sink {
    init = Queue.create;
    push = (fun acc x -> Queue.add x acc; acc);
    full = (fun _ -> false);
    stop = (fun acc -> acc);
  }


let sum =
  Sink {
    init = (fun () -> 0);
    push = (+);
    full = (fun _ -> false);
    stop = (fun x -> x);
  }


let product =
  Sink {
    init = (fun () -> 1);
    push = (fun acc x -> acc * x);
    full = (fun x -> x = 0);
    stop = (fun x -> x);
  }


let bytes =
  Sink {
    init = (fun () -> Buffer.create 128);
    push = (fun buf x -> Buffer.add_bytes buf x; buf);
    full = (fun _ -> false);
    stop = (fun buf -> Buffer.to_bytes buf);
  }


let string =
  Sink {
    init = (fun () -> Buffer.create 128);
    push = (fun buf x -> Buffer.add_string buf x; buf);
    full = (fun _ -> false);
    stop = (fun buf -> Buffer.contents buf);
  }


let file path =
  Sink {
    init = (fun () -> Stdlib.open_out path);
    push = (fun chan x -> Stdlib.output_string chan (x ^ "\n"); chan);
    full = (fun _ -> false);
    stop = close_out
  }


let stderr =
  Sink {
    init = (fun () -> ());
    push = (fun () x -> output_string Pervasives.stderr (x ^ "\n"); flush Pervasives.stderr);
    full = (fun _ -> false);
    stop = (fun () -> ());
  }


let stdout =
  Sink {
    init = (fun () -> ());
    push = (fun () x -> output_string Pervasives.stdout (x ^ "\n"); flush Pervasives.stdout);
    full = (fun _ -> false);
    stop = (fun () -> ());
  }


let dispose (Sink snk) =
  snk.stop (snk.init ())


module Syntax = struct
  let (let+) f t = map t f
  let (and+) a b = zip a b
end



module Functor = struct
  type ('a, 'b) t = ('a, 'b) sink

  let map  = map
end


module Applicative = struct
  type ('a, 'b) t = ('a, 'b) sink

  let pure = fill

  let (<*>) (Sink l) (Sink r) =
    let init () = (l.init (), r.init ()) in
    let push (l_acc, r_acc) x = (l.push l_acc x, r.push r_acc x) in
    let full (l_acc, r_acc) = (l.full l_acc || r.full r_acc) in
    let stop (l_acc, r_acc) = l.stop l_acc (r.stop r_acc) in
    Sink { init; push; full; stop }
end

OCaml

Innovation. Community. Security.