1Seq(3)                           OCaml library                          Seq(3)
2
3
4

NAME

6       Seq - Sequences.
7

Module

9       Module   Seq
10

Documentation

12       Module Seq
13        : sig end
14
15
16       Sequences.
17
18       A  sequence  of type 'a Seq.t can be thought of as a delayed list, that
19       is, a list whose elements are computed only when they are demanded by a
20       consumer.  This  allows sequences to be produced and transformed lazily
21       (one element at a time) rather than eagerly  (all  elements  at  once).
22       This also allows constructing conceptually infinite sequences.
23
24       The  type  'a  Seq.t  is defined as a synonym for unit -> 'a Seq.node .
25       This is a function type: therefore, it  is  opaque.  The  consumer  can
26       query  a  sequence  in  order  to request the next element (if there is
27       one), but cannot otherwise inspect the sequence in any way.
28
29       Because it is opaque, the type 'a Seq.t does not reveal whether  a  se‐
30       quence is:
31
32       -persistent, which means that the sequence can be used as many times as
33       desired, producing the same elements every time, just like an immutable
34       list; or
35
36       -ephemeral,  which means that the sequence is not persistent.  Querying
37       an ephemeral sequence might have an observable side effect, such as in‐
38       crementing  a  mutable counter.  As a common special case, an ephemeral
39       sequence can be affine, which means that it must  be  queried  at  most
40       once.
41
42       It also does not reveal whether the elements of the sequence are:
43
44
45       -pre-computed  and  stored in memory, which means that querying the se‐
46       quence is cheap;
47
48       -computed when first demanded and then stored in  memory,  which  means
49       that querying the sequence once can be expensive, but querying the same
50       sequence again is cheap; or
51
52       -re-computed every time they are demanded, which  may  or  may  not  be
53       cheap.
54
55       It  is up to the programmer to keep these distinctions in mind so as to
56       understand the time and space requirements of sequences.
57
58       For the sake of simplicity, most of the documentation that  follows  is
59       written  under  the  implicit assumption that the sequences at hand are
60       persistent.  We normally do not point out when or how many  times  each
61       function  is invoked, because that would be too verbose.  For instance,
62       in the description of map , we write: "if xs is the  sequence  x0;  x1;
63       ...  then map f xs is the sequence f x0; f x1; ...  ".  If we wished to
64       be more explicit, we could point  out  that  the  transformation  takes
65       place  on  demand:  that is, the elements of map f xs are computed only
66       when they are demanded. In other words, the definition let ys =  map  f
67       xs  terminates  immediately and does not invoke f . The function call f
68       x0 takes place only when the first element of ys is demanded,  via  the
69       function call ys() .  Furthermore, calling ys() twice causes f x0 to be
70       called twice as well. If one wishes for f to be applied at most once to
71       each  element  of  xs , even in scenarios where ys is queried more than
72       once, then one should use let ys = memoize (map f xs) .
73
74       As a general rule, the functions that build sequences, such  as  map  ,
75       filter  , scan , take , etc., produce sequences whose elements are com‐
76       puted only on demand. The functions  that  eagerly  consume  sequences,
77       such  as  is_empty  ,  find , length , iter , fold_left , etc., are the
78       functions that force computation to take place.
79
80       When possible, we recommend  using  sequences  rather  than  dispensers
81       (functions  of  type  unit  -> 'a option that produce elements upon de‐
82       mand). Whereas sequences can be persistent or ephemeral, dispensers are
83       always  ephemeral,  and  are typically more difficult to work with than
84       sequences. Two conversion functions, Seq.to_dispenser  and  Seq.of_dis‐
85       penser , are provided.
86
87
88       Since 4.07
89
90
91
92
93
94       type 'a t = unit -> 'a node
95
96
97       A  sequence  xs of type 'a t is a delayed list of elements of type 'a .
98       Such a sequence is queried by performing a function application xs()  .
99       This function application returns a node, allowing the caller to deter‐
100       mine whether the sequence is empty or nonempty, and in the latter case,
101       to obtain its head and tail.
102
103
104       type 'a node =
105        | Nil
106        | Cons of 'a * 'a t
107
108
109       A  node is either Nil , which means that the sequence is empty, or Cons
110       (x, xs) , which means that x is the first element of the  sequence  and
111       that xs is the remainder of the sequence.
112
113
114
115
116   Consuming sequences
117       The  functions  in this section consume their argument, a sequence, ei‐
118       ther partially or completely:
119
120       - is_empty and uncons consume the sequence down to depth 1.   That  is,
121       they demand the first argument of the sequence, if there is one.
122
123       - iter , fold_left , length , etc., consume the sequence all the way to
124       its end. They terminate only if the sequence is finite.
125
126       - for_all , exists , find , etc. consume the sequence down to a certain
127       depth, which is a priori unpredictable.
128
129       Similarly, among the functions that consume two sequences, one can dis‐
130       tinguish two groups:
131
132       - iter2 and fold_left2 consume both sequences all the way to  the  end,
133       provided the sequences have the same length.
134
135       -  for_all2 , exists2 , equal , compare consume the sequences down to a
136       certain depth, which is a priori unpredictable.
137
138       The functions that consume two sequences can  be  applied  to  two  se‐
139       quences  of  distinct lengths: in that case, the excess elements in the
140       longer sequence are ignored. (It may be the case that one  excess  ele‐
141       ment is demanded, even though this element is not used.)
142
143       None of the functions in this section is lazy. These functions are con‐
144       sumers: they force some computation to take place.
145
146       val is_empty : 'a t -> bool
147
148
149       is_empty xs determines whether the sequence xs is empty.
150
151       It is recommended that the sequence xs be persistent.  Indeed, is_empty
152       xs demands the head of the sequence xs , so, if xs is ephemeral, it may
153       be the case that xs cannot be used any more after this call  has  taken
154       place.
155
156
157       Since 4.14
158
159
160
161       val uncons : 'a t -> ('a * 'a t) option
162
163       If xs is empty, then uncons xs is None .
164
165       If  xs  is nonempty, then uncons xs is Some (x, ys) where x is the head
166       of the sequence and ys its tail.
167
168
169       Since 4.14
170
171
172
173       val length : 'a t -> int
174
175
176       length xs is the length of the sequence xs .
177
178       The sequence xs must be finite.
179
180
181       Since 4.14
182
183
184
185       val iter : ('a -> unit) -> 'a t -> unit
186
187
188       iter f xs invokes f x successively for every element x of the  sequence
189       xs , from left to right.
190
191       It terminates only if the sequence xs is finite.
192
193
194
195       val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
196
197
198       fold_left  f _ xs invokes f _ x successively for every element x of the
199       sequence xs , from left to right.
200
201       An accumulator of type 'a is threaded through the calls to f .
202
203       It terminates only if the sequence xs is finite.
204
205
206
207       val iteri : (int -> 'a -> unit) -> 'a t -> unit
208
209
210       iteri f xs invokes f i x successively for every element  x  located  at
211       index i in the sequence xs .
212
213       It terminates only if the sequence xs is finite.
214
215
216       iteri  f  xs  is equivalent to iter (fun (i, x) -> f i x) (zip (ints 0)
217       xs) .
218
219
220       Since 4.14
221
222
223
224       val fold_lefti : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
225
226
227       fold_lefti f _ xs invokes f _ i x successively for every element x  lo‐
228       cated at index i of the sequence xs .
229
230       An accumulator of type 'b is threaded through the calls to f .
231
232       It terminates only if the sequence xs is finite.
233
234
235       fold_lefti  f  accu xs is equivalent to fold_left (fun accu (i, x) -> f
236       accu i x) accu (zip (ints 0) xs) .
237
238
239       Since 4.14
240
241
242
243       val for_all : ('a -> bool) -> 'a t -> bool
244
245
246       for_all p xs determines whether all elements x of the sequence xs  sat‐
247       isfy p x .
248
249       The sequence xs must be finite.
250
251
252       Since 4.14
253
254
255
256       val exists : ('a -> bool) -> 'a t -> bool
257
258
259       exists  xs  p determines whether at least one element x of the sequence
260       xs satisfies p x .
261
262       The sequence xs must be finite.
263
264
265       Since 4.14
266
267
268
269       val find : ('a -> bool) -> 'a t -> 'a option
270
271
272       find p xs returns Some x , where x is the first element of the sequence
273       xs that satisfies p x , if there is such an element.
274
275       It returns None if there is no such element.
276
277       The sequence xs must be finite.
278
279
280       Since 4.14
281
282
283
284       val find_map : ('a -> 'b option) -> 'a t -> 'b option
285
286
287       find_map  f xs returns Some y , where x is the first element of the se‐
288       quence xs such that f x = Some _ , if there is  such  an  element,  and
289       where y is defined by f x = Some y .
290
291       It returns None if there is no such element.
292
293       The sequence xs must be finite.
294
295
296       Since 4.14
297
298
299
300       val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
301
302
303       iter2  f xs ys invokes f x y successively for every pair (x, y) of ele‐
304       ments drawn synchronously from the sequences xs and ys .
305
306       If the sequences xs and ys have different lengths, then iteration stops
307       as  soon as one sequence is exhausted; the excess elements in the other
308       sequence are ignored.
309
310       Iteration terminates only if at least one of the sequences xs and ys is
311       finite.
312
313
314       iter2 f xs ys is equivalent to iter (fun (x, y) -> f x y) (zip xs ys) .
315
316
317       Since 4.14
318
319
320
321       val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b t -> 'c t -> 'a
322
323
324       fold_left2 f _ xs ys invokes f _ x y successively for every pair (x, y)
325       of elements drawn synchronously from the sequences xs and ys .
326
327       An accumulator of type 'a is threaded through the calls to f .
328
329       If the sequences xs and ys have different lengths, then iteration stops
330       as  soon as one sequence is exhausted; the excess elements in the other
331       sequence are ignored.
332
333       Iteration terminates only if at least one of the sequences xs and ys is
334       finite.
335
336
337       fold_left2  f accu xs ys is equivalent to fold_left (fun accu (x, y) ->
338       f accu x y) (zip xs ys) .
339
340
341       Since 4.14
342
343
344
345       val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
346
347
348       for_all2 p xs ys determines whether all pairs (x, y) of elements  drawn
349       synchronously from the sequences xs and ys satisfy p x y .
350
351       If the sequences xs and ys have different lengths, then iteration stops
352       as soon as one sequence is exhausted; the excess elements in the  other
353       sequence  are  ignored.   In  particular,  if  xs  or ys is empty, then
354       for_all2 p xs ys is true. This is  where  for_all2  and  equal  differ:
355       equal eq xs ys can be true only if xs and ys have the same length.
356
357       At least one of the sequences xs and ys must be finite.
358
359
360       for_all2 p xs ys is equivalent to for_all (fun b -> b) (map2 p xs ys) .
361
362
363       Since 4.14
364
365
366
367       val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
368
369
370       exists2  p  xs ys determines whether some pair (x, y) of elements drawn
371       synchronously from the sequences xs and ys satisfies p x y .
372
373       If the sequences xs and ys have different lengths, then iteration  must
374       stop  as  soon as one sequence is exhausted; the excess elements in the
375       other sequence are ignored.
376
377       At least one of the sequences xs and ys must be finite.
378
379
380       exists2 p xs ys is equivalent to exists (fun b -> b) (map2 p xs ys) .
381
382
383       Since 4.14
384
385
386
387       val equal : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
388
389       Provided the function eq defines an equality on elements, equal  eq  xs
390       ys determines whether the sequences xs and ys are pointwise equal.
391
392       At least one of the sequences xs and ys must be finite.
393
394
395       Since 4.14
396
397
398
399       val compare : ('a -> 'b -> int) -> 'a t -> 'b t -> int
400
401       Provided  the  function cmp defines a preorder on elements, compare cmp
402       xs ys compares the sequences xs and ys according to  the  lexicographic
403       preorder.
404
405       For more details on comparison functions, see Array.sort .
406
407       At least one of the sequences xs and ys must be finite.
408
409
410       Since 4.14
411
412
413
414
415   Constructing sequences
416       The  functions in this section are lazy: that is, they return sequences
417       whose elements are computed only when demanded.
418
419       val empty : 'a t
420
421
422       empty is the empty sequence.  It has no elements. Its length is 0.
423
424
425
426       val return : 'a -> 'a t
427
428
429       return x is the sequence whose sole element is x .  Its length is 1.
430
431
432
433       val cons : 'a -> 'a t -> 'a t
434
435
436       cons x xs is the sequence that begins with the  element  x  ,  followed
437       with the sequence xs .
438
439       Writing  cons (f()) xs causes the function call f() to take place imme‐
440       diately. For this call to be delayed until the sequence is queried, one
441       must instead write (fun () -> Cons(f(), xs)) .
442
443
444       Since 4.11
445
446
447
448       val init : int -> (int -> 'a) -> 'a t
449
450
451       init n f is the sequence f 0; f 1; ...; f (n-1) .
452
453
454       n must be nonnegative.
455
456       If  desired, the infinite sequence f 0; f 1; ...  can be defined as map
457       f (ints 0) .
458
459
460       Since 4.14
461
462
463       Raises Invalid_argument if n is negative.
464
465
466
467       val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
468
469
470       unfold constructs a sequence out of a  step  function  and  an  initial
471       state.
472
473       If  f  u is None then unfold f u is the empty sequence.  If f u is Some
474       (x, u') then unfold f u is the nonempty sequence cons x (unfold f u') .
475
476       For example, unfold (function [] -> None | h :: t -> Some (h, t)) l  is
477       equivalent to List.to_seq l .
478
479
480       Since 4.11
481
482
483
484       val repeat : 'a -> 'a t
485
486
487       repeat  x  is the infinite sequence where the element x is repeated in‐
488       definitely.
489
490
491       repeat x is equivalent to cycle (return x) .
492
493
494       Since 4.14
495
496
497
498       val forever : (unit -> 'a) -> 'a t
499
500
501       forever f is an infinite sequence where every element is  produced  (on
502       demand) by the function call f() .
503
504       For  instance,  forever  Random.bool  is an infinite sequence of random
505       bits.
506
507
508       forever f is equivalent to map f (repeat ()) .
509
510
511       Since 4.14
512
513
514
515       val cycle : 'a t -> 'a t
516
517
518       cycle xs is the infinite sequence that consists of an  infinite  number
519       of repetitions of the sequence xs .
520
521       If xs is an empty sequence, then cycle xs is empty as well.
522
523       Consuming  (a  prefix  of) the sequence cycle xs once can cause the se‐
524       quence xs to be consumed more than once.  Therefore, xs must be persis‐
525       tent.
526
527
528       Since 4.14
529
530
531
532       val iterate : ('a -> 'a) -> 'a -> 'a t
533
534
535       iterate  f x is the infinite sequence whose elements are x , f x , f (f
536       x) , and so on.
537
538       In other words, it is the orbit of the function f , starting at x .
539
540
541       Since 4.14
542
543
544
545
546   Transforming sequences
547       The functions in this section are lazy: that is, they return  sequences
548       whose elements are computed only when demanded.
549
550       val map : ('a -> 'b) -> 'a t -> 'b t
551
552
553       map f xs is the image of the sequence xs through the transformation f .
554
555       If  xs is the sequence x0; x1; ...  then map f xs is the sequence f x0;
556       f x1; ...  .
557
558
559
560       val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
561
562
563       mapi is analogous to map , but applies the function f to an  index  and
564       an element.
565
566
567       mapi f xs is equivalent to map2 f (ints 0) xs .
568
569
570       Since 4.14
571
572
573
574       val filter : ('a -> bool) -> 'a t -> 'a t
575
576
577       filter p xs is the sequence of the elements x of xs that satisfy p x .
578
579       In  other  words, filter p xs is the sequence xs , deprived of the ele‐
580       ments x such that p x is false.
581
582
583
584       val filter_map : ('a -> 'b option) -> 'a t -> 'b t
585
586
587       filter_map f xs is the sequence of the elements y such that f x =  Some
588       y , where x ranges over xs .
589
590
591       filter_map  f xs is equivalent to map Option.get (filter Option.is_some
592       (map f xs)) .
593
594
595
596       val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t
597
598       If xs is a sequence [x0; x1; x2; ...]  , then scan f a0  xs  is  a  se‐
599       quence of accumulators [a0; a1; a2; ...]  where a1 is f a0 x0 , a2 is f
600       a1 x1 , and so on.
601
602       Thus, scan f a0 xs is conceptually related to fold_left f a0 xs .  How‐
603       ever,  instead of performing an eager iteration and immediately return‐
604       ing the final accumulator, it returns a sequence of accumulators.
605
606       For instance, scan (+) 0 transforms a sequence of integers into the se‐
607       quence of its partial sums.
608
609       If xs has length n then scan f a0 xs has length n+1 .
610
611
612       Since 4.14
613
614
615
616       val take : int -> 'a t -> 'a t
617
618
619       take n xs is the sequence of the first n elements of xs .
620
621       If xs has fewer than n elements, then take n xs is equivalent to xs .
622
623
624       n must be nonnegative.
625
626
627       Since 4.14
628
629
630       Raises Invalid_argument if n is negative.
631
632
633
634       val drop : int -> 'a t -> 'a t
635
636
637       drop n xs is the sequence xs , deprived of its first n elements.
638
639       If xs has fewer than n elements, then drop n xs is empty.
640
641
642       n must be nonnegative.
643
644
645       drop  is  lazy:  the first n+1 elements of the sequence xs are demanded
646       only when the first element of drop n xs is demanded. For this  reason,
647       drop 1 xs is not equivalent to tail xs , which queries xs immediately.
648
649
650       Since 4.14
651
652
653       Raises Invalid_argument if n is negative.
654
655
656
657       val take_while : ('a -> bool) -> 'a t -> 'a t
658
659
660       take_while  p  xs  is the longest prefix of the sequence xs where every
661       element x satisfies p x .
662
663
664       Since 4.14
665
666
667
668       val drop_while : ('a -> bool) -> 'a t -> 'a t
669
670
671       drop_while p xs is the sequence xs , deprived of the prefix  take_while
672       p xs .
673
674
675       Since 4.14
676
677
678
679       val group : ('a -> 'a -> bool) -> 'a t -> 'a t t
680
681       Provided  the  function eq defines an equality on elements, group eq xs
682       is the sequence of the maximal runs of adjacent duplicate  elements  of
683       the sequence xs .
684
685       Every element of group eq xs is a nonempty sequence of equal elements.
686
687       The concatenation concat (group eq xs) is equal to xs .
688
689       Consuming  group  eq xs , and consuming the sequences that it contains,
690       can cause xs to be consumed more than once. Therefore, xs must be  per‐
691       sistent.
692
693
694       Since 4.14
695
696
697
698       val memoize : 'a t -> 'a t
699
700       The sequence memoize xs has the same elements as the sequence xs .
701
702       Regardless of whether xs is ephemeral or persistent, memoize xs is per‐
703       sistent: even if it is queried several times, xs  is  queried  at  most
704       once.
705
706       The  construction  of the sequence memoize xs internally relies on sus‐
707       pensions provided by the  module  Lazy  .  These  suspensions  are  not
708       thread-safe.  Therefore, the sequence memoize xs must not be queried by
709       multiple threads concurrently.
710
711
712       Since 4.14
713
714
715
716       exception Forced_twice
717
718
719       This exception is raised when a sequence returned  by  Seq.once  (or  a
720       suffix of it) is queried more than once.
721
722
723       Since 4.14
724
725
726
727       val once : 'a t -> 'a t
728
729       The sequence once xs has the same elements as the sequence xs .
730
731       Regardless  of  whether  xs  is  ephemeral or persistent, once xs is an
732       ephemeral sequence: it can be queried at most once.  If it (or a suffix
733       of  it)  is  queried more than once, then the exception Forced_twice is
734       raised. This can be useful, while debugging or testing, to ensure  that
735       a sequence is consumed at most once.
736
737
738       Since 4.14
739
740
741       Raises  Forced_twice  if  once  xs , or a suffix of it, is queried more
742       than once.
743
744
745
746       val transpose : 'a t t -> 'a t t
747
748       If xss is a matrix (a sequence of rows), then transpose xss is the  se‐
749       quence of the columns of the matrix xss .
750
751       The rows of the matrix xss are not required to have the same length.
752
753       The matrix xss is not required to be finite (in either direction).
754
755       The matrix xss must be persistent.
756
757
758       Since 4.14
759
760
761
762
763   Combining sequences
764       val append : 'a t -> 'a t -> 'a t
765
766
767       append xs ys is the concatenation of the sequences xs and ys .
768
769       Its elements are the elements of xs , followed by the elements of ys .
770
771
772       Since 4.11
773
774
775
776       val concat : 'a t t -> 'a t
777
778       If  xss  is  a sequence of sequences, then concat xss is its concatena‐
779       tion.
780
781       If xss is the sequence xs0; xs1; ...  then concat xss is  the  sequence
782       xs0 @ xs1 @ ...  .
783
784
785       Since 4.13
786
787
788
789       val flat_map : ('a -> 'b t) -> 'a t -> 'b t
790
791
792       flat_map f xs is equivalent to concat (map f xs) .
793
794
795
796       val concat_map : ('a -> 'b t) -> 'a t -> 'b t
797
798
799       concat_map f xs is equivalent to concat (map f xs) .
800
801
802       concat_map is an alias for flat_map .
803
804
805       Since 4.13
806
807
808
809       val zip : 'a t -> 'b t -> ('a * 'b) t
810
811
812       zip  xs ys is the sequence of pairs (x, y) drawn synchronously from the
813       sequences xs and ys .
814
815       If the sequences xs and ys have different lengths,  then  the  sequence
816       ends  as  soon as one sequence is exhausted; the excess elements in the
817       other sequence are ignored.
818
819
820       zip xs ys is equivalent to map2 (fun a b -> (a, b)) xs ys .
821
822
823       Since 4.14
824
825
826
827       val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
828
829
830       map2 f xs ys is the sequence of the elements f x y ,  where  the  pairs
831       (x, y) are drawn synchronously from the sequences xs and ys .
832
833       If  the  sequences  xs and ys have different lengths, then the sequence
834       ends as soon as one sequence is exhausted; the excess elements  in  the
835       other sequence are ignored.
836
837
838       map2 f xs ys is equivalent to map (fun (x, y) -> f x y) (zip xs ys) .
839
840
841       Since 4.14
842
843
844
845       val interleave : 'a t -> 'a t -> 'a t
846
847
848       interleave  xs ys is the sequence that begins with the first element of
849       xs , continues with the first element of ys , and so on.
850
851       When one of the sequences xs and ys is exhausted, interleave xs ys con‐
852       tinues with the rest of the other sequence.
853
854
855       Since 4.14
856
857
858
859       val sorted_merge : ('a -> 'a -> int) -> 'a t -> 'a t -> 'a t
860
861       If  the  sequences xs and ys are sorted according to the total preorder
862       cmp , then sorted_merge cmp xs ys is the sorted  sequence  obtained  by
863       merging the sequences xs and ys .
864
865       For more details on comparison functions, see Array.sort .
866
867
868       Since 4.14
869
870
871
872       val product : 'a t -> 'b t -> ('a * 'b) t
873
874
875       product xs ys is the Cartesian product of the sequences xs and ys .
876
877       For  every element x of xs and for every element y of ys , the pair (x,
878       y) appears once as an element of product xs ys .
879
880       The order in which the pairs appear is unspecified.
881
882       The sequences xs and ys are not required to be finite.
883
884       The sequences xs and ys must be persistent.
885
886
887       Since 4.14
888
889
890
891       val map_product : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
892
893       The sequence map_product f xs ys is the image through f of  the  Carte‐
894       sian product of the sequences xs and ys .
895
896       For every element x of xs and for every element y of ys , the element f
897       x y appears once as an element of map_product f xs ys .
898
899       The order in which these elements appear is unspecified.
900
901       The sequences xs and ys are not required to be finite.
902
903       The sequences xs and ys must be persistent.
904
905
906       map_product f xs ys is equivalent to map (fun (x, y) -> f x y) (product
907       xs ys) .
908
909
910       Since 4.14
911
912
913
914
915   Splitting a sequence into two sequences
916       val unzip : ('a * 'b) t -> 'a t * 'b t
917
918
919       unzip transforms a sequence of pairs into a pair of sequences.
920
921
922       unzip xs is equivalent to (map fst xs, map snd xs) .
923
924       Querying  either  of the sequences returned by unzip xs causes xs to be
925       queried.  Therefore, querying both of them  causes  xs  to  be  queried
926       twice.   Thus,  xs  must  be  persistent and cheap.  If that is not the
927       case, use unzip (memoize xs) .
928
929
930       Since 4.14
931
932
933
934       val split : ('a * 'b) t -> 'a t * 'b t
935
936
937       split is an alias for unzip .
938
939
940       Since 4.14
941
942
943
944       val partition_map : ('a -> ('b, 'c) Either.t) -> 'a t -> 'b t * 'c t
945
946
947       partition_map f xs returns a pair of sequences (ys, zs) , where:
948
949
950       - ys is the sequence of the elements y such that f x = Left y , where x
951       ranges over xs ;
952
953
954       -  zs is the sequence of the elements z such that f x = Right z , where
955       x ranges over xs .
956
957       partition_map  f  xs  is  equivalent  to  a  pair  of  filter_map   Ei‐
958       ther.find_left (map f xs) and filter_map Either.find_right (map f xs) .
959
960       Querying  either of the sequences returned by partition_map f xs causes
961       xs to be queried.  Therefore, querying both of them  causes  xs  to  be
962       queried  twice.  Thus, xs must be persistent and cheap.  If that is not
963       the case, use partition_map f (memoize xs) .
964
965
966       Since 4.14
967
968
969
970       val partition : ('a -> bool) -> 'a t -> 'a t * 'a t
971
972
973       partition p xs returns a pair of the subsequence of the elements of  xs
974       that  satisfy  p  and the subsequence of the elements of xs that do not
975       satisfy p .
976
977
978       partition p xs is equivalent to filter p xs, filter (fun x  ->  not  (p
979       x)) xs .
980
981       Consuming both of the sequences returned by partition p xs causes xs to
982       be consumed twice and causes the function f to be applied twice to each
983       element  of the list.  Therefore, f should be pure and cheap.  Further‐
984       more, xs should be persistent and cheap.  If that is not the case,  use
985       partition p (memoize xs) .
986
987
988       Since 4.14
989
990
991
992
993   Converting between sequences and dispensers
994       A  dispenser  is  a  representation of a sequence as a function of type
995       unit -> 'a option . Every time this function is invoked, it returns the
996       next  element  of the sequence. When there are no more elements, it re‐
997       turns None . A dispenser  has  mutable  internal  state,  therefore  is
998       ephemeral:  the  sequence  that  it  represents can be consumed at most
999       once.
1000
1001       val of_dispenser : (unit -> 'a option) -> 'a t
1002
1003
1004       of_dispenser it is the sequence of the elements produced  by  the  dis‐
1005       penser  it  .  It  is an ephemeral sequence: it can be consumed at most
1006       once. If a persistent sequence is needed, use memoize (of_dispenser it)
1007       .
1008
1009
1010       Since 4.14
1011
1012
1013
1014       val to_dispenser : 'a t -> unit -> 'a option
1015
1016
1017       to_dispenser xs is a fresh dispenser on the sequence xs .
1018
1019       This  dispenser has mutable internal state, which is not protected by a
1020       lock; so, it must not be used by several threads concurrently.
1021
1022
1023       Since 4.14
1024
1025
1026
1027
1028   Sequences of integers
1029       val ints : int -> int t
1030
1031
1032       ints i is the infinite sequence of the  integers  beginning  at  i  and
1033       counting up.
1034
1035
1036       Since 4.14
1037
1038
1039
1040
1041
1042OCamldoc                          2023-07-20                            Seq(3)
Impressum