package cfstream

  1. Overview
  2. Docs

Source file CFStream_stream.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
open Core_kernel
include Stream

let next_exn = next

let next s =
  try Some (next_exn s) with
  | Stream.Failure -> None
;;

let npeek s n = npeek n s

let is_empty s =
  match peek s with
  | None -> true
  | Some _ -> false
;;

let empty () = from (const None)
let to_stream x = x
let of_stream x = x

exception Expected_streams_of_equal_length
exception Premature_end_of_input

let of_list l =
  let lr = ref l in
  let f _ =
    match !lr with
    | h :: t ->
      lr := t;
      Some h
    | [] -> None
  in
  from f
;;

let rec iteri xs ~f =
  match peek xs with
  | Some x ->
    f (count xs) x;
    junk xs;
    iteri xs ~f
  | None -> ()
;;

let iter xs ~f = iteri xs ~f:(const f)

let rec iter2i_exn xs ys ~f =
  match peek xs, peek ys with
  | Some x, Some y ->
    f (count xs) (count ys) x y;
    junk xs;
    junk ys;
    iter2i_exn xs ys ~f
  | None, None -> ()
  | _, _ -> raise Expected_streams_of_equal_length
;;

let iter2_exn xs ys ~f = iter2i_exn xs ys ~f:(const (const f))

let iter2i a b ~f =
  try iter2i_exn a b ~f with
  | Expected_streams_of_equal_length -> ()
;;

let iter2 a b ~f =
  try iter2_exn a b ~f with
  | Expected_streams_of_equal_length -> ()
;;

let rec find_map xs ~f =
  match next xs with
  | Some x -> (
    match f x with
    | Some _ as y -> y
    | None -> find_map xs ~f)
  | None -> None
;;

let find xs ~f = find_map xs ~f:(fun x -> if f x then Some x else None)

let find_exn xs ~f =
  match find xs ~f with
  | Some x -> x
  | None -> raise Caml.Not_found
;;

let exists xs ~f =
  match find xs ~f with
  | Some _ -> true
  | None -> false
;;

let rec for_all xs ~f =
  match next xs with
  | Some x when not (f x) -> false
  | Some _ -> for_all xs ~f
  | None -> true
;;

let rec foldi xs ~init ~f =
  match next xs with
  | None -> init
  | Some x -> foldi xs ~init:(f (count xs - 1) init x) ~f
;;

(* [count xs - 1] because of the call to [next], which increased the
     stream count by one *)

let fold xs ~init ~f = foldi xs ~init ~f:(const f)

let reduce xs ~f =
  match next xs with
  | Some init -> fold xs ~init ~f
  | None -> invalid_arg "Stream.reduce: stream should contain at least one element"
;;

let sum = reduce ~f:( + )
let fsum = reduce ~f:( +. )

let rec fold2i_exn xs ys ~init ~f =
  match next xs, next ys with
  | Some x, Some y ->
    let init = f (count xs - 1) (count ys - 1) init x y in
    (* decrease by one because of the calls to [next] *)
    fold2i_exn xs ys ~init ~f
  | None, None -> init
  | _ -> raise Expected_streams_of_equal_length
;;

let fold2_exn xs ys ~init ~f = fold2i_exn xs ys ~init ~f:(const (const f))

let rec fold2i xs ys ~init ~f =
  match next xs, next ys with
  | Some x, Some y ->
    let init = f (count xs - 1) (count ys - 1) init x y in
    fold2i xs ys ~init ~f
  | _ -> init
;;

let fold2 xs ys ~init ~f = fold2i xs ys ~init ~f:(const (const f))

let scanl xs ~init ~f =
  let current = ref init in
  let f i =
    if i = 0
    then Some init
    else (
      match next xs with
      | Some x ->
        current := f !current x;
        Some !current
      | None -> None)
  in
  from f
;;

let scan xs ~f =
  match next xs with
  | Some init -> scanl xs ~init ~f
  | None -> empty ()
;;

let take_whilei xs ~f =
  let aux i =
    match peek xs with
    | Some x when f i x ->
      junk xs;
      Some x
    | _ -> None
  in
  from aux
;;

let take_while xs ~f = take_whilei xs ~f:(const f)
let take xs ~n = take_whilei xs ~f:(fun j _ -> j < n)

let rec drop_whilei xs ~f =
  match peek xs with
  | Some x when f (count xs) x ->
    junk xs;
    drop_whilei xs ~f
  | _ -> ()
;;

let drop_while xs ~f = drop_whilei xs ~f:(const f)

let drop xs ~n =
  let i = ref n in
  drop_whilei xs ~f:(fun _ _ ->
      if !i > 0
      then (
        decr i;
        true)
      else false)
;;

let skip_whilei xs ~f =
  drop_whilei xs ~f;
  xs
;;

let skip_while xs ~f = skip_whilei xs ~f:(const f)

let skip xs ~n =
  drop xs ~n;
  xs
;;

let span xs ~f =
  (*Two possibilities: either the tail has been read
      already -- in which case all head data has been
      copied onto the queue -- or the tail hasn't been
      read -- in which case, stuff should be read from
      [xs] *)
  let queue = Queue.create ()
  and read_from_queue = ref false in
  let head _ =
    if !read_from_queue
    then
      (* Everything from the head has been copied *)
      Queue.dequeue queue
      (* to the queue already                     *)
    else (
      match peek xs with
      | Some x as e when f x ->
        junk xs;
        e
      | _ -> None)
  and tail _ =
    if not !read_from_queue
    then (
      (*Copy everything to the queue         *)
      read_from_queue := true;
      let rec aux () =
        match peek xs with
        | Some x when f x ->
          Queue.enqueue queue x;
          aux ()
        | e -> e
      in
      aux ())
    else next xs
  in
  from head, from tail
;;

let group_aux xs map eq =
  let prev_group_force = ref ignore in
  let for_each_group _ =
    !prev_group_force ();
    match next xs with
    | None -> None
    | Some x ->
      let queue = Queue.create ()
      and forced = ref false
      and mapped_x = map x in
      let aux i =
        if i = 0
        then Some x
        else if !forced
        then Queue.dequeue queue
        else (
          match peek xs with
          | Some y as e when eq (map y) mapped_x ->
            junk xs;
            e
          | _ -> None)
      in
      let force () =
        forced := true;
        let rec loop () =
          match peek xs with
          | Some y when eq (map y) mapped_x ->
            junk xs;
            Queue.enqueue queue y;
            loop ()
          | _ -> ()
        in
        loop ()
      in
      prev_group_force := force;
      Some (from aux)
  in
  from for_each_group
;;

let group xs ~f = group_aux xs f Poly.( = )
let group_by xs ~eq = group_aux xs ident eq

let chunk2 xs =
  from (fun _ ->
      match next xs with
      | None -> None
      | Some a -> (
        match next xs with
        | None -> raise Premature_end_of_input
        | Some b -> Some (a, b)))
;;

let chunk3 xs =
  from (fun _ ->
      match next xs with
      | None -> None
      | Some a -> (
        match next xs with
        | None -> raise Premature_end_of_input
        | Some b -> (
          match next xs with
          | None -> raise Premature_end_of_input
          | Some c -> Some (a, b, c))))
;;

let chunk4 xs =
  from (fun _ ->
      match next xs with
      | None -> None
      | Some a -> (
        match next xs with
        | None -> raise Premature_end_of_input
        | Some b -> (
          match next xs with
          | None -> raise Premature_end_of_input
          | Some c -> (
            match next xs with
            | None -> raise Premature_end_of_input
            | Some d -> Some (a, b, c, d)))))
;;

let mapi xs ~f =
  let aux i = Option.map (next xs) ~f:(f i) in
  from aux
;;

let map xs ~f =
  let aux _ = Option.map (next xs) ~f in
  from aux
;;

let mapi2_exn xs ys ~f =
  let aux i =
    match peek xs, peek ys with
    | Some x, Some y ->
      let r = f i x y in
      junk xs;
      junk ys;
      Some r
    | None, None -> None
    | _, _ -> raise Expected_streams_of_equal_length
  in
  from aux
;;

let map2_exn xs ys ~f = mapi2_exn xs ys ~f:(fun _ -> f)

let filter xs ~f =
  let rec aux i =
    match next xs with
    | Some x when not (f x) -> aux i
    | x -> x
  in
  from aux
;;

let filter_map xs ~f =
  let rec aux i =
    match next xs with
    | Some x -> (
      match f x with
      | None -> aux i
      | x -> x)
    | None -> None
  in
  from aux
;;

let append xs ys =
  let aux _ =
    match next xs with
    | None -> next ys
    | e -> e
  in
  from aux
;;

let concat xs =
  let rec find_next_non_empty_stream xs =
    match peek xs with
    | Some stream when is_empty stream ->
      junk xs;
      find_next_non_empty_stream xs
    | x -> x
  in
  let current = ref (empty ()) in
  let aux _ =
    match next !current with
    | None -> (
      match find_next_non_empty_stream xs with
      | None -> None
      | Some stream ->
        current := stream;
        next stream)
    | x -> x
  in
  from aux
;;

let concat_map l ~f =
  let rec find_next_non_empty_stream xs =
    (* As opposed to concat, we use next here to avoid infinite looping. *)
    match next xs with
    | Some x ->
      let stream = f x in
      if is_empty stream then find_next_non_empty_stream xs else Some stream
    | None -> None
  in
  let current = ref (empty ()) in
  let aux _ =
    match next !current with
    | None -> (
      match find_next_non_empty_stream l with
      | None -> None
      | Some stream ->
        current := stream;
        next stream)
    | x -> x
  in
  from aux
;;

let combine (xs, ys) =
  let aux _ =
    match peek xs, peek ys with
    | Some x, Some y ->
      junk xs;
      junk ys;
      Some (x, y)
    | _ -> None
  in
  from aux
;;

let uncombine xs =
  let whosfirst = ref `left
  and lq = Queue.create ()
  and rq = Queue.create () in
  let rec left i =
    match !whosfirst with
    | `left -> (
      match next xs with
      | None -> None
      | Some (l, r) ->
        Queue.enqueue rq r;
        Some l)
    | `right -> (
      match Queue.dequeue lq with
      | None ->
        whosfirst := `left;
        left i
      | x -> x)
  and right i =
    match !whosfirst with
    | `right -> (
      match next xs with
      | None -> None
      | Some (l, r) ->
        Queue.enqueue lq l;
        Some r)
    | `left -> (
      match Queue.dequeue rq with
      | None ->
        whosfirst := `right;
        right i
      | x -> x)
  in
  from left, from right
;;

let merge xs ys ~cmp =
  let aux _ =
    match peek xs, peek ys with
    | (Some x as ex), Some y when cmp x y <= 0 ->
      junk xs;
      ex
    | Some _, (Some _ as ey) ->
      junk ys;
      ey
    | (Some _ as ex), None ->
      junk xs;
      ex
    | None, (Some _ as ey) ->
      junk ys;
      ey
    | None, None -> None
  in
  from aux
;;

let partition xs ~f =
  let pos_queue = Queue.create ()
  and neg_queue = Queue.create () in
  let rec pos i =
    match Queue.dequeue pos_queue with
    | None -> (
      match next xs with
      | Some x when not (f x) ->
        Queue.enqueue neg_queue x;
        pos i
      | e -> e)
    | e -> e
  and neg i =
    match Queue.dequeue neg_queue with
    | None -> (
      match next xs with
      | Some x when f x ->
        Queue.enqueue pos_queue x;
        neg i
      | e -> e)
    | e -> e
  in
  from pos, from neg
;;

let uniq xs =
  match peek xs with
  | None -> empty ()
  | Some first ->
    let prev = ref first in
    let rec aux i =
      if i = 0
      then Some first
      else (
        match next xs with
        | None -> None
        | Some x ->
          if Poly.(x = !prev)
          then aux i
          else (
            prev := x;
            Some x))
    in
    from aux
;;

let init n ~f =
  if n < 0
  then empty ()
  else (
    let aux i = if i < n then Some (f i) else None in
    from aux)
;;

let singleton x = init 1 ~f:(const x)
let to_list t = List.rev (fold ~init:[] ~f:(fun l b -> b :: l) t)

let result_to_exn s ~error_to_exn =
  from (fun _ ->
      match next s with
      | None -> None
      | Some result -> (
        match result with
        | Ok x -> Some x
        | Result.Error x -> raise (error_to_exn x)))
;;

let unfoldi init ~f =
  let a = ref init in
  from (fun i ->
      match f i !a with
      | Some (b, a_next) ->
        a := a_next;
        Some b
      | None -> None)
;;

let unfold init ~f = unfoldi init ~f:(const f)

let range ?until n =
  let stop = Option.value_map until ~default:(fun _ -> false) ~f:( < ) in
  unfold n ~f:(fun i -> if stop i then None else Some (i, i + 1))
;;

let of_lazy s =
  let next _ = next (Lazy.force s) in
  from next
;;

(* Default buffer_size set to UNIX_BUFFER_SIZE in OCaml's
   otherlibs/unix/unixsupport.h, but unsure if this is a good
   choice. *)
let strings_of_channel ?(buffer_size = 65536) inp =
  let buf = Bytes.create buffer_size in
  from (fun _ ->
      match In_channel.input inp ~buf ~pos:0 ~len:buffer_size with
      | 0 -> None
      | len -> Some (Bytes.To_string.sub buf ~pos:0 ~len))
;;

let of_array a =
  Stream.from (fun i ->
      try Some a.(i) with
      | Invalid_argument _ -> None)
;;

let to_array strm = Array.of_list (to_list strm)
let of_hashtbl t = of_list (Hashtbl.to_alist t)

let to_hashtbl xs =
  let t = Hashtbl.Poly.create () in
  iter xs ~f:(fun (key, data) -> Hashtbl.Poly.set t ~key ~data);
  t
;;

let of_map t = of_list (Map.to_alist t)

let to_map xs =
  fold xs ~init:Map.Poly.empty ~f:(fun accu (key, data) -> Map.Poly.set accu ~key ~data)
;;

let of_set t = of_list (Set.to_list t)
let to_set xs = fold xs ~init:Set.Poly.empty ~f:(fun accu e -> Set.Poly.add accu e)

module Infix = struct
  let ( -- ) x y = range x ~until:y

  let ( --. ) (a, step) b =
    let n = Int.of_float ((b -. a) /. step) + 1 in
    if n < 0 then empty () else init n ~f:(fun i -> (Float.of_int i *. step) +. a)
  ;;

  let ( --^ ) x y = range x ~until:(y - 1)

  let ( --- ) x y =
    if x <= y
    then x -- y
    else unfold x ~f:(fun prev -> if prev >= y then Some (prev, prev - 1) else None)
  ;;

  let ( /@ ) x f = map x ~f
  let ( // ) x f = filter x ~f
  let ( //@ ) x f = filter_map x ~f
end

module Result = struct
  let stream_map = map
  let stream_map2_exn = map2_exn
  let stream_fold = fold

  type ('a, 'b) t = ('a, 'b) Result.t Stream.t

  module Impl = struct
    let all_gen (type e) g (xs : ('a, e) t) ~f =
      let module M = struct
        exception E of e
      end
      in
      let error_to_exn e = M.E e in
      try g (f (result_to_exn xs ~error_to_exn)) with
      | M.E e -> Result.Error e
    ;;

    let all xs ~f = all_gen ident xs ~f
    let all' xs ~f = all_gen (fun x -> Ok x) xs ~f
    let to_exn = result_to_exn

    let map' rs ~f =
      let f = function
        | Ok x -> Ok (f x)
        | Error _ as e -> e
      in
      stream_map rs ~f
    ;;

    let map rs ~f =
      let f = function
        | Ok x -> f x
        | Error _ as e -> e
      in
      stream_map rs ~f
    ;;

    let map2_exn xs ys ~f =
      let f x y =
        match x, y with
        | Ok x, Ok y -> f x y
        | (Error _ as ex), _ -> ex
        | _, (Error _ as ey) -> ey
      in
      stream_map2_exn xs ys ~f
    ;;

    let map2_exn' xs ys ~f =
      let f x y =
        match x, y with
        | Ok x, Ok y -> Ok (f x y)
        | (Error _ as ex), _ -> ex
        | _, (Error _ as ey) -> ey
      in
      stream_map2_exn xs ys ~f
    ;;

    let fold' (type e) rs ~init ~f =
      let module M = struct
        exception E of e
      end
      in
      let f accu = function
        | Ok x -> f accu x
        | Error e -> raise (M.E e)
      in
      try Ok (stream_fold rs ~init ~f) with
      | M.E e -> Error e
    ;;

    let fold (type e) rs ~init ~f =
      let module M = struct
        exception E of e
      end
      in
      let f accu = function
        | Ok x -> (
          match f accu x with
          | Ok r -> r
          | Error e -> raise (M.E e))
        | Error e -> raise (M.E e)
      in
      try Ok (stream_fold rs ~init ~f) with
      | M.E e -> Error e
    ;;
  end

  include Impl
end

module Or_error = struct
  type 'a t = 'a Or_error.t Stream.t

  include Result.Impl
end
OCaml

Innovation. Community. Security.