oni / cf / cf_seq.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
(*---------------------------------------------------------------------------*
  $Change$
  Copyright (c) 2002-2010, James H. Woodyatt
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
    Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    
    Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  OF THE POSSIBILITY OF SUCH DAMAGE. 
 *---------------------------------------------------------------------------*)

type 'a t = 'a cell Lazy.t and 'a cell = P of 'a * 'a t | Z

exception Empty

let nil = Lazy.lazy_from_val Z

let head s =
    match Lazy.force s with
    | P (hd, _) -> hd
    | Z -> raise Empty

let tail s =
    match Lazy.force s with
    | P (_, tl) -> tl
    | Z -> raise Empty

let rec concat s1 s2 =
    lazy begin
        match Lazy.force s1 with
        | P (hd, tl) -> P (hd, concat tl s2)
        | Z -> Lazy.force s2
    end

let rec flatten z =
    lazy begin
        match Lazy.force z with
        | Z -> Z
        | P (hd, tl) ->
            match Lazy.force hd with
            | P (hd2, tl2) -> P (hd2, flatten (lazy (P (tl2, tl))))
            | Z -> Lazy.force (flatten tl)
    end

let limit =
    let rec loop ?x n s =
        match Lazy.force s with
        | Z -> Z
        | P (hd, tl) when n > 0 -> P (hd, lazy (loop (pred n) tl))
        | _ ->
            match x with
            | None -> Z
            | Some x -> raise x
    in
    fun ?x n s ->
        if n < 0 then invalid_arg "Cf_seq.limit: n < 0";
        lazy (loop ?x n s)

let shift =
    let rec loop n = function
        | P (_, tl) when n > 0 -> loop (pred n) (Lazy.force tl)
        | cell -> cell
    in
    fun n s ->
        if n < 0 then invalid_arg "Cf_seq.shift: n < 0";
        lazy (loop n (Lazy.force s))

let rec sentinel x z =
    lazy begin
        match Lazy.force z with
        | Z -> raise x
        | P (hd, tl) -> P (hd, sentinel x tl)
    end

let reverse =
    let rec loop stack = function
        | P (hd, tl) -> loop (hd :: stack) (Lazy.force tl)
        | Z -> stack
    in
    fun s ->
        loop [] (Lazy.force s)

let length =
    let rec loop n = function
        | P (_, y) -> loop (succ n) (Lazy.force y)
        | Z -> n
    in
    fun s ->
        loop 0 (Lazy.force s)

let rec unfold f x =
    lazy begin
        match f x with
        | Some (y, x) -> P (y, unfold f x)
        | None -> Z
    end

let rec unfold2 f x =
    lazy begin
        match f x with
        | Some (_, x as y) -> P (y, unfold2 f x)
        | None -> Z
    end

let rec iterate f s =
    match Lazy.force s with
    | P (hd, tl) -> f hd; iterate f tl
    | Z -> ()

let rec predicate f s =
    match Lazy.force s with
    | P (hd, tl) -> f hd && predicate f tl
    | Z -> true

let rec constrain f s =
    lazy begin
        match Lazy.force s with
        | P (hd, tl) when f hd -> P (hd, constrain f tl)
        | _ -> Z
    end

let search f =
    let rec loop n s =
        match Lazy.force s with
        | P (hd, tl) -> if f hd then n else loop (succ n) tl
        | Z -> n
    in
    fun s ->
        loop 0 s

let rec fold f m s =
    match Lazy.force s with
    | P (hd, tl) -> fold f (f m hd) tl
    | Z -> m

let filter =
    let rec loop f s =
        match Lazy.force s with
        | P (hd, tl) when f hd -> P (hd, lazy (loop f tl))
        | P (_, tl) -> loop f tl
        | Z -> Z
    in
    fun f s ->
        lazy (loop f s)

let rec map f s =
    lazy begin
        match Lazy.force s with
        | P (hd, tl) -> P (f hd, map f tl)
        | Z -> Z
    end

let optmap =
    let rec loop f s =
        match Lazy.force s with
        | Z -> Z
        | P (hd, tl) ->
            match f hd with
            | Some y -> P (y, lazy (loop f tl))
            | None -> loop f tl
    in
    fun f s ->
        lazy (loop f s)

let listmap =
    let rec inner f cont = function
        | hd :: tl -> P (hd, lazy (inner f cont tl))
        | [] -> outer f cont
    and outer f s =
        match Lazy.force s with
        | P (hd, tl) -> inner f tl (f hd)
        | Z -> Z
    in
    fun f s ->
        lazy (outer f s)

let seqmap =
    let rec inner f cont s =
        match Lazy.force s with
        | P (hd, tl) -> P (hd, lazy (inner f cont tl))
        | Z -> outer f cont
    and outer f s =
        match Lazy.force s with
        | P (hd, tl) -> inner f tl (f hd)
        | Z -> Z
    in
    fun f s ->
        lazy (outer f s)

let partition f s = filter f s, filter (fun x -> not (f x)) s

let rec fcmp f s0 s1 =
    match Lazy.force s0, Lazy.force s1 with
    | P (hd1, tl1), P (hd2, tl2) ->
        let d = f hd1 hd2 in
        if d <> 0 then d else fcmp f tl1 tl2
    | P _, Z -> 1
    | Z, P _ -> -1
    | Z, Z -> 0

let cmp s0 s1 = fcmp Pervasives.compare s0 s1

let equal s0 s1 = cmp s0 s1 = 0

let rec first s =
    lazy begin
        match Lazy.force s with
        | P ((hd, _), tl) -> P (hd, first tl)
        | Z -> Z
    end

let rec second s =
    lazy begin
        match Lazy.force s with
        | P ((_, hd), tl) -> P (hd, second tl)
        | Z -> Z
    end

let split s = first s, second s

let rec combine s0 s1 =
    lazy begin
        match Lazy.force s0, Lazy.force s1 with
        | P (hd1, tl1), P (hd2, tl2) -> P ((hd1, hd2), combine tl1 tl2)
        | _, _ -> Z
    end

let rec weave f c s =
    lazy begin
        match Lazy.force s with
        | P (hd, tl) -> let c, hd = f c hd in P (hd, weave f c tl)
        | Z -> Z
    end

let rec optweave =
    let rec loop f c s =
        match Lazy.force s with
        | Z -> Z
        | P (hd, tl) ->
            let c, hd = f c hd in
            match hd with
            | Some hd -> P (hd, lazy (loop f c tl))
            | None -> loop f c tl
    in
    let start f c s = lazy (loop f c s) in
    start

let rec iterate2 f s0 s1 =
    match Lazy.force s0, Lazy.force s1 with
    | P (hd1, tl1), P (hd2, tl2) -> f hd1 hd2; iterate2 f tl1 tl2
    | _, _ -> ()

let rec predicate2 f s0 s1 =
    match Lazy.force s0, Lazy.force s1 with
    | P (hd1, tl1), P (hd2, tl2) -> f hd1 hd2 && predicate2 f tl1 tl2
    | Z, Z -> true
    | _, _ -> false

let rec constrain2 f s0 s1 =
    lazy begin
        match Lazy.force s0, Lazy.force s1 with
        | P (hd1, tl1), P (hd2, tl2) when f hd1 hd2 ->
            P ((hd1, hd2), constrain2 f tl1 tl2)
        | _, _ ->
            Z
    end

let rec fold2 f m s0 s1 =
    match Lazy.force s0, Lazy.force s1 with
    | P (hd1, tl1), P (hd2, tl2) -> fold2 f (f m hd1 hd2) tl1 tl2
    | _, _ -> m

let filter2 =
    let rec loop f s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) when f hd1 hd2 ->
            P ((hd1, hd2), lazy (loop f tl1 tl2))
        | P (_, tl1), P (_, tl2) ->
            loop f tl1 tl2
        | _, _ ->
            Z
    in
    fun f s1 s2 ->
        lazy (loop f s1 s2)

let rec map2 f s1 s2 =
    lazy begin
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) -> P (f hd1 hd2, map2 f tl1 tl2)
        | _, _ -> Z
    end

let optmap2 =
    let rec loop f s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) ->
            begin
                match f hd1 hd2 with
                | Some y -> P (y, lazy (loop f tl1 tl2))
                | None -> loop f tl1 tl2
            end
        | _, _ ->
            Z
    in
    fun f s1 s2 ->
        lazy (loop f s1 s2)

let listmap2 =
    let rec inner f c1 c2 = function
        | hd :: tl -> P (hd, lazy (inner f c1 c2 tl))
        | [] -> outer f c1 c2
    and outer f s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) -> inner f tl1 tl2 (f hd1 hd2)
        | _, _ -> Z
    in
    fun f s1 s2 ->
        lazy (outer f s1 s2)

let seqmap2 =
    let rec inner f c1 c2 s =
        match Lazy.force s with
        | P (hd, tl) -> P (hd, lazy (inner f c1 c2 tl))
        | Z -> outer f c1 c2
    and outer f s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) -> inner f tl1 tl2 (f hd1 hd2)
        | _, _ -> Z
    in
    fun f s1 s2 ->
        lazy (outer f s1 s2)

let rec weave2 =
    let rec loop f c s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) ->
            let c, hd3 = f c hd1 hd2 in P (hd3, lazy (loop f c tl1 tl2))
        | _, _ ->
            Z
    in
    let start f c sa sb = lazy (loop f c sa sb) in
    start

let rec optweave2 =
    let rec loop f c s1 s2 =
        match Lazy.force s1, Lazy.force s2 with
        | P (hd1, tl1), P (hd2, tl2) ->
            let c, v = f c hd1 hd2 in begin
                match v with
                | Some hd -> P (hd, lazy (loop f c tl1 tl2))
                | None -> loop f c tl1 tl2
            end
        | _, _ ->
            Z
    in
    let start f c s1 s2 = lazy (loop f c s1 s2) in
    start

let rec of_channel c =
    lazy (try P (input_char c, of_channel c) with End_of_file -> Z)

let of_substring s =
    let len = String.length s in
    let rec loop pos =
        let pos = succ pos in
        if pos < len then P (String.unsafe_get s pos, lazy (loop pos)) else Z
    in
    fun pos ->
        lazy (loop (pred pos))

let of_string s = of_substring s 0

let of_subarray a =
    let len = Array.length a in
    let rec loop pos =
        let pos = succ pos in
        if pos < len then P (Array.unsafe_get a pos, lazy (loop pos)) else Z
    in
    fun pos ->
        lazy (loop (pred pos))

let of_array a = of_subarray a 0

let rec of_function f = lazy (try P (f (), of_function f) with Not_found -> Z)

let rec of_list = function
    | [] -> Lazy.lazy_from_val Z
    | hd :: tl -> lazy (P (hd, of_list tl))

let rec to_channel s c =
    match Lazy.force s with
    | P (hd, tl) ->
        output_char c hd;
        to_channel tl c
    | Z ->
        ()

let to_string =
    let rec loop b = function
        | P (hd, tl) ->
            Buffer.add_char b hd;
            loop b (Lazy.force tl)
        | Z ->
            ()
    in
    fun s ->
        let b = Buffer.create 32 in
        loop b (Lazy.force s);
        Buffer.contents b

let to_substring =
    let rec loop str pos stop = function
        | P (hd, tl) when pos < stop ->
            String.unsafe_set str pos hd;
            loop str (succ pos) stop (Lazy.force tl)
        | x ->
            x
    in
    fun s str pos len ->
        if pos < 0 then
            invalid_arg "Cf_seq.to_substring: pos < 0";
        if pos + len > String.length str then
            invalid_arg "Cf_seq.to_substring: pos + len > String.length str";
        lazy (loop str pos len (Lazy.force s))

let to_array =
    let f cr _ =
        match !cr with
        | Z -> assert false
        | P (hd, tl) -> cr := Lazy.force tl; hd
    in
    fun s ->
        let len = length s in
        match Lazy.force s with
        | P _ as cell -> Array.init len (f (ref cell))
        | Z -> [| |]

let to_subarray =
    let rec loop v pos stop = function
        | P (hd, tl) when pos < stop ->
            Array.unsafe_set v pos hd;
            loop v (succ pos) stop (Lazy.force tl)
        | x ->
            x
    in
    fun s v pos len ->
        if pos < 0 then
            invalid_arg "Cf_seq.to_subarray: pos < 0";
        if pos + len > Array.length v then
            invalid_arg "Cf_seq.to_subarray: pos + len > Array.length v";
        lazy (loop v pos len (Lazy.force s))

let to_list s = List.rev (reverse s)

let rec to_buffer s b =
    match Lazy.force s with
    | P (hd, tl) ->
        Buffer.add_char b hd;
        to_buffer tl b
    | Z ->
        ()

let to_function s =
    let s0 = ref s in
    let rec loop () =
        match Lazy.force !s0 with
        | P (hd, tl) -> s0 := tl; hd
        | Z -> raise End_of_file
    in
    loop

let finishC_ _ = Lazy.lazy_from_val Z
let finishSC_ _ _ = Lazy.lazy_from_val Z

let writeC o f = lazy (P (o, f ()))
let evalC m = m finishC_

let writeSC o f s = lazy (P (o, f () s))
let evalSC m s = m finishSC_ s

module S = struct
    open Cf_smonad
    open Op
    
    let accumulate =
        let rec loop stack seq =
            match Lazy.force seq with
            | P (hd, tl) -> hd >>= fun x -> loop (x :: stack) tl
            | Z -> return (List.rev stack)
        in
        fun s -> loop [] s
    
    let rec sequence s =
        match Lazy.force s with
        | P (hd, tl) -> hd >>= fun _ -> sequence tl
        | Z -> return ()
end

module C = struct
    open Cf_cmonad
    open Op
    
    let accumulate =
        let rec loop stack seq =
            match Lazy.force seq with
            | P (hd, tl) -> hd >>= fun x -> loop (x :: stack) tl
            | Z -> return (List.rev stack)
        in
        fun s -> loop [] s
    
    let rec sequence s =
        match Lazy.force s with
        | P (hd, tl) -> hd >>= fun _ -> sequence tl
        | Z -> return ()
end

module SC = struct
    open Cf_scmonad
    open Op
    
    let accumulate =
        let rec loop stack seq =
            match Lazy.force seq with
            | P (hd, tl) -> hd >>= fun x -> loop (x :: stack) tl
            | Z -> return (List.rev stack)
        in
        fun s -> loop [] s
    
    let rec sequence s =
        match Lazy.force s with
        | P (hd, tl) -> hd >>= fun _ -> sequence tl
        | Z -> return ()
end

(*--- $File$ ---*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.