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productJ@#Seq!t!a@RC@F@@@H@#Seq!t!b@TC@I@@@K#Seq!t@$@@@L@@@N@@O@@P@ n^=^= o^=^f@@ mx@@+map_productK@@!a@dC@U@!b@fC@V!c@hC@W@@X@@Y@#Seq!t@@@[@#Seq!t!@@@]#Seq!t$@@@_@@`@@a@@b@ `` ``<@@ y@@%unzipL@#Seq!t@!a@uC@i@!b@wC@j@@k@@@m@#Seq!t@@@o@#Seq!t@@@q@@r@@s@ bb bb@@ z@@%splitM@#Seq!t@!a@C@x@!b@C@y@@z@@@|@#Seq!t@@@~@#Seq!t@@@@@@@@ *dndn +dnd@@ ){@@-partition_map`@@!a@ uC@&Stdlib&Either!t!b@ wC@ e!c@ yC@ f@@@ i@@ j@#Seq!t(@@@ l@#Seq!t%@@@ n@#Seq!t)@@@ p@@ q@@ r@@ s@ xdd yde@@ w|@@)partitiona@@!a@ C@ z >@@@ {@@ |@#Seq!t@@@ ~@#Seq!t"@@@ @#Seq!t-@@@ @@ @@ @@ @ gg gh0@@ }@@,of_dispenserb@@ @@@  X!a@ C@ @@@ @@ #Seq!t@@@ @@ @ lVlV lVl@@ ~@@,to_dispenserc@#Seq!t!a@ C@ @@@ @ @@@  @@@ @@ @@ @  mm  mm@@ @@$intsd@ m@@@ #Seq!t z@@@ @@@ @@ @ nn nn@@ @@@@3*Sequences.@7 A sequence of type ('a Seq.t8 can be thought of as a ,delayed list@ #, that is, a list whose elements are computed only when they are demanded by a consumer. This allows sequences to be produced and transformed lazily (one element at a time) rather than eagerly (all elements at once). This also allows constructing conceptually infinite sequences.@- The type ('a Seq.t= is defined as a synonym for 3unit -> 'a Seq.node H. This is a function type: therefore, it is opaque. The consumer can ( query@ | a sequence in order to request the next element (if there is one), but cannot otherwise inspect the sequence in any way.@ # Because it is opaque, the type ('a Seq.t& does #not@ ! reveal whether a sequence is:*persistent@ , which means that the sequence can be used as many times as desired, producing the same elements every time, just like an immutable list; or@)ephemeral@ , which means that the sequence is not persistent. Querying an ephemeral sequence might have an observable side effect, such as incrementing a mutable counter. As a common special case, an ephemeral sequence can be &affine@ 8, which means that it must be queried at most once.@@1 It also does #not@ 1 reveal whether the elements of the sequence are:@7pre-computed and stored@ A in memory, which means that querying the sequence is cheap;@ ,computed when first demanded and then stored@ in memory, which means that querying the sequence once can be expensive, but querying the same sequence again is cheap; or@ (re-computed every time they are demanded@ %, which may or may not be cheap.@@ It is up to the programmer to keep these distinctions in mind so as to understand the time and space requirements of sequences.@ For the sake of simplicity, most of the documentation that follows is written under the implicit assumption that the sequences at hand are persistent. We normally do not point out $when@$ or .how many times@ h each function is invoked, because that would be too verbose. For instance, in the description of #map3, we write: "if "xs1 is the sequence +x0; x1; ...* then (map f xs1 is the sequence /f x0; f x1; ... ". If we wished to be more explicit, we could point out that the transformation takes place on demand: that is, the elements of (map f xs P are computed only when they are demanded. In other words, the definition 1let ys = map f xs / terminates immediately and does not invoke !f4. The function call $f x0 / takes place only when the first element of "ys $ is demanded, via the function call $ys():. Furthermore, calling $ys(). twice causes $f x0 2 to be called twice as well. If one wishes for !f 2 to be applied at most once to each element of "xs:, even in scenarios where "ys 3 is queried more than once, then one should use ;let ys = memoize (map f xs)!.@ C As a general rule, the functions that build sequences, such as #map%, &filter", $scan", $take , etc., produce sequences whose elements are computed only on demand. The functions that eagerly consume sequences, such as (is_empty", $find", &length", $iter", )fold_left B, etc., are the functions that force computation to take place.@ ] When possible, we recommend using sequences rather than dispensers (functions of type 1unit -> 'a option that produce elements upon demand). Whereas sequences can be persistent or ephemeral, dispensers are always ephemeral, and are typically more difficult to work with than sequences. Two conversion functions, 0Seq.to_dispenserD@( and 0Seq.of_dispenserD@/, are provided.@@@@$4.07@@@@@@@A4../../stdlib/seq.mli#%Seq.t3+A sequence "xs) of type $'a t + is a delayed list of elements of type "'a F. Such a sequence is queried by performing a function application $xs() . This function application returns a node, allowing the caller to determine whether the sequence is empty or nonempty, and in the latter case, to obtain its head and tail.@@@@@@@@@@@@ Y@@A @  t@@#(Seq.node31A node is either #Nil 1, which means that the sequence is empty, or ,Cons (x, xs)3, which means that !x 3 is the first element of the sequence and that "xs " is the remainder of the sequence.@@@@@@@@@@@@ Y@ @@@   @@@@A@@0@A@3Consuming sequences@@ dThe functions in this section consume their argument, a sequence, either partially or completely:(is_empty% and &uncons u consume the sequence down to depth 1. That is, they demand the first argument of the sequence, if there is one.@$iter", )fold_left", &length h, etc., consume the sequence all the way to its end. They terminate only if the sequence is finite.@'for_all", &exists", $find Z, etc. consume the sequence down to a certain depth, which is a priori unpredictable.@@ a Similarly, among the functions that consume two sequences, one can distinguish two groups:%iter2% and *fold_left2 a consume both sequences all the way to the end, provided the sequences have the same length.@(for_all2", 'exists2", %equal", 'compare U consume the sequences down to a certain depth, which is a priori unpredictable.@@  The functions that consume two sequences can be applied to two sequences of distinct lengths: in that case, the excess elements in the longer sequence are ignored. (It may be the case that one excess element is demanded, even though this element is not used.)@  None of the functions in this section is lazy. These functions are consumers: they force some computation to take place.@,Seq.is_empty3+is_empty xs ! determines whether the sequence "xs* is empty.@ ) It is recommended that the sequence "xs< be persistent. Indeed, +is_empty xs " demands the head of the sequence "xs-, so, if "xs ' is ephemeral, it may be the case that "xs = cannot be used any more after this call has taken place.@@@@$4.14@@@@@@@ A@  C@@@@ 0*Seq.uncons3#If "xs0 is empty, then )uncons xs$ is $None!.@( If "xs3 is nonempty, then )uncons xs$ is ,Some (x, ys)' where !x % is the head of the sequence and "ys* its tail.@@@@$4.14@@@@@@@ g@F h@@@@ E*Seq.length3)length xs? is the length of the sequence "xs!.@; Does not terminate if "xs- is infinite.@@@@$4.14@@@@@@@ [@j \@@@@ I(Seq.iter3)iter f xs) invokes #f x $ successively for every element !x1 of the sequence "xs9, from left to right.@ ( It terminates only if the sequence "xs+ is finite.@@@@@@@@@@@@ i@ j@ ^@@@@ R-Seq.fold_left30fold_left f _ xs) invokes %f _ x $ successively for every element !x1 of the sequence "xs9, from left to right.@< An accumulator of type "'a " is threaded through the calls to !f!.@ ( It terminates only if the sequence "xs+ is finite.@@@@@@@@@@@@ @ @ @ u@@@@ n)Seq.iteri3*iteri f xs) invokes %f i x $ successively for every element !x2 located at index !i1 in the sequence "xs!.@ ( It terminates only if the sequence "xs+ is finite.@% *iteri f xs6 is equivalent to ,iter (fun (i, x) -> f i x) (zip (ints 0) xs)!.@@@@$4.14@@@@@@@ @% @( @@@@ .Seq.fold_lefti31fold_lefti f _ xs) invokes 'f _ i x $ successively for every element !x2 located at index !i1 of the sequence "xs!.@< An accumulator of type "'b " is threaded through the calls to !f!.@ ( It terminates only if the sequence "xs+ is finite.@% 4fold_lefti f accu xs6 is equivalent to @fold_left (fun accu (i, x) -> f accu i x) accu (zip (ints 0) xs)!.@@@@$4.14@@@@@@@ @~ @ @ @@@@ +Seq.for_all3,for_all p xs ! determines whether all elements !x1 of the sequence "xs- satisfy #p x!.@: May not terminate if "xs- is infinite.@@@@$4.14@@@@@@@ @ @ @@@@ *Seq.exists3+exists p xs ) determines whether at least one element !x5 of the sequence "xs+ satisfies #p x!.@: May not terminate if "xs- is infinite.@@@@$4.14@@@@@@@ @ @ @@@@ ˠ(Seq.find3)find p xs) returns &Some x(, where !x * is the first element of the sequence "xs0 that satisfies #p x>, if there is such an element.@0 It returns $None= if there is no such element.@: May not terminate if "xs- is infinite.@@@@$4.14@@@@@@@ @* @- @@@@ 栕.Seq.find_index3/find_index p xs) returns &Some i(, where !i 7 is the index of the first element of the sequence "xs0 that satisfies #p x ", if there is such an element.@0 It returns $None= if there is no such element.@: May not terminate if "xs- is infinite.@@@@#5.1@@@@@@@@m@p @@@@ ,Seq.find_map3-find_map f xs) returns &Some y(, where !x * is the first element of the sequence "xs+ such that ,f x = Some _ -, if there is such an element, and where !y/ is defined by ,f x = Some y!.@0 It returns $None= if there is no such element.@: May not terminate if "xs- is infinite.@@@@$4.14@@@@@@@:@;@)@@@@-Seq.find_mapi3(Same as (find_map , but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.@9 May not terminate if "xs- is infinite.@@@@#5.1@@@@@@@0@1@@@@@ )Seq.iter23-iter2 f xs ys) invokes %f x y= successively for every pair &(x, y) 8 of elements drawn synchronously from the sequences "xs% and "ys!.@6 If the sequences "xs% and "ys have different lengths, then iteration stops as soon as one sequence is exhausted; the excess elements in the other sequence are ignored.@ D Iteration terminates only if at least one of the sequences "xs% and "ys+ is finite.@% -iter2 f xs ys6 is equivalent to &iter (fun (x, y) -> f x y) (zip xs ys)!.@@@@$4.14@@@@@@@Z@?[@BG@E?@@@@3.Seq.fold_left234fold_left2 f _ xs ys) invokes 'f _ x y ! successively for every pair &(x, y) 8 of elements drawn synchronously from the sequences "xs% and "ys!.@< An accumulator of type "'a " is threaded through the calls to !f!.@6 If the sequences "xs% and "ys have different lengths, then iteration stops as soon as one sequence is exhausted; the excess elements in the other sequence are ignored.@ D Iteration terminates only if at least one of the sequences "xs% and "ys+ is finite.@% 7fold_left2 f accu xs ys6 is equivalent to 5fold_left (fun accu (x, y) -> f accu x y) (zip xs ys)!.@@@@$4.14@@@@@@@@@@|@t@@@@m,Seq.for_all230for_all2 p xs ys> determines whether all pairs &(x, y) 8 of elements drawn synchronously from the sequences "xs% and "ys) satisfy %p x y!.@6 If the sequences "xs% and "ys have different lengths, then iteration stops as soon as one sequence is exhausted; the excess elements in the other sequence are ignored. In particular, if "xs$ or "ys4 is empty, then 0for_all2 p xs ys< is true. This is where (for_all2% and %equal) differ: .equal eq xs ys9 can be true only if "xs% and "ys6 have the same length.@ 0 May not terminate if both of the sequences "xs% and "ys. are infinite.@% 0for_all2 p xs ys2 is equivalent to #for_all (fun b -> b) (map2 p xs ys)!.@@@@$4.14@@@@@@@@F@I@L@@@@Ġ+Seq.exists23/exists2 p xs ys> determines whether some pair &(x, y) 8 of elements drawn synchronously from the sequences "xs% and "ys+ satisfies %p x y!.@6 If the sequences "xs% and "ys have different lengths, then iteration must stop as soon as one sequence is exhausted; the excess elements in the other sequence are ignored.@ 0 May not terminate if both of the sequences "xs% and "ys. are infinite.@% /exists2 p xs ys2 is equivalent to "exists (fun b -> b) (map2 p xs ys)!.@@@@$4.14@@@@@@@@@@@@@@렕)Seq.equal36Provided the function "eq & defines an equality on elements, .equal eq xs ys " determines whether the sequences "xs% and "ys9 are pointwise equal.@ 0 May not terminate if both of the sequences "xs% and "ys. are infinite.@@@@$4.14@@@@@@@@@@@@@@+Seq.compare36Provided the function #cmp % defines a preorder on elements, 1compare cmp xs ys8 compares the sequences "xs% and "ys - according to the lexicographic preorder.@ 3 For more details on comparison functions, see *Array.sort@@!.@ 0 May not terminate if both of the sequences "xs% and "ys. are infinite.@@@@$4.14@@@@@@@$@0%@3@6 @@@@A@6Constructing sequences@@ zThe functions in this section are lazy: that is, they return sequences whose elements are computed only when demanded.@)Seq.empty3%empty @ is the empty sequence. It has no elements. Its length is 0.@@@@@@@@@@@@ @@@@*Seq.return3(return x ' is the sequence whose sole element is !x6. Its length is 1.@@@@@@@@@@@@ @i @@@@(Seq.cons3)cons x xs . is the sequence that begins with the element !x !, followed with the sequence "xs!.@- Writing -cons (f()) xs: causes the function call #f() z to take place immediately. For this call to be delayed until the sequence is queried, one must instead write 9(fun () -> Cons(f(), xs))!.@@@@$4.11@@@@@@@&@'@"@@@@-Seq.singleton3+singleton x 2 returns the one-element sequence containing only !x!.@@@@#5.4@@@@@@@@@@@@(Seq.init3(init n f1 is the sequence 6f 0; f 1; ...; f (n-1)!.@% !n5 must be nonnegative.@ ' If desired, the infinite sequence -f 0; f 1; ...7 can be defined as .map f (ints 0)!.@@@@$4.14@@@0Invalid_argument#if !n- is negative.@@@@@D@E@A@@@@-*Seq.unfold3&unfold G constructs a sequence out of a step function and an initial state.@( If #f u$ is $None* then *unfold f u? is the empty sequence. If #f u$ is ,Some (x, u')* then *unfold f u: is the nonempty sequence 4cons x (unfold f u')!.@2 For example, 6unfold (function [] -> None | h :: t -> Some (h, t)) l6 is equivalent to -List.to_seq l!.@@@@$4.11@@@@@@@q@Qr@Ts@@@@S*Seq.repeat3(repeat x 0 is the infinite sequence where the element !x: is repeated indefinitely.@% (repeat x2 is equivalent to 0cycle (return x)!.@@@@$4.14@@@@@@@o@~p@@@@c+Seq.forever3)forever f ^ is an infinite sequence where every element is produced (on demand) by the function call #f()!.@7 For instance, 3forever Random.bool ( is an infinite sequence of random bits.@% )forever f2 is equivalent to 1map f (repeat ())!.@@@@$4.14@@@@@@@@@@@@v)Seq.cycle3(cycle xs a is the infinite sequence that consists of an infinite number of repetitions of the sequence "xs!.@( If "xs is an empty sequence, then (cycle xs2 is empty as well.@ * Consuming (a prefix of) the sequence (cycle xs ! once can cause the sequence "xs / to be consumed more than once. Therefore, "xs4 must be persistent.@@@@$4.14@@@@@@@@@@@@+Seq.iterate3+iterate f x 1 is the infinite sequence whose elements are !x", #f x", 'f (f x),, and so on.@ 5 In other words, it is the orbit of the function !f2, starting at !x!.@@@@$4.14@@@@@@@@(@+@@@@A@6Transforming sequences@@ zThe functions in this section are lazy: that is, they return sequences whose elements are computed only when demanded.@'Seq.map3(map f xs> is the image of the sequence "xs through the transformation !f!.@( If "xs1 is the sequence +x0; x1; ...* then (map f xs1 is the sequence /f x0; f x1; ...!.@@@@@@@@@@@@@q@t@@@@ʠ(Seq.mapi3$mapi1 is analogous to #map;, but applies the function !f to an index and an element.@% )mapi f xs2 is equivalent to 2map2 f (ints 0) xs!.@@@@$4.14@@@@@@@@@@@@@ɠ*Seq.filter3+filter p xs ! is the sequence of the elements !x$ of "xs2 that satisfy #p x!.@5 In other words, +filter p xs1 is the sequence "xs?, deprived of the elements !x+ such that #p x* is false.@@@@@@@@@@@@@@@@@@ࠕ+Seq.filteri3(Same as LD@ , but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.@@@@#5.4@@@@@@@@@@@@@Ǡ.Seq.filter_map3/filter_map f xs ! is the sequence of the elements !y/ such that ,f x = Some y(, where !x- ranges over "xs!.@% /filter_map f xs6 is equivalent to 1map Option.get (filter Option.is_some (map f xs))!.@@@@@@@@@@@@@>@A@@@@Ҡ(Seq.scan3#If "xs/ is a sequence 1[x0; x1; x2; ...]+, then ,scan f a0 xs # is a sequence of accumulators 1[a0; a1; a2; ...]+ where "a1$ is 'f a0 x0", "a2$ is 'f a1 x1,, and so on.@+ Thus, ,scan f a0 xs is conceptually related to 1fold_left f a0 xs . However, instead of performing an eager iteration and immediately returning the final accumulator, it returns a sequence of accumulators.@3 For instance, *scan (+) 0 M transforms a sequence of integers into the sequence of its partial sums.@( If "xs, has length !n* then ,scan f a0 xs, has length #n+1!.@@@@$4.14@@@@@@@;@<@=@.@@@@(Seq.take3)take n xs> is the sequence of the first !n- elements of "xs!.@( If "xs0 has fewer than !n4 elements, then )take n xs2 is equivalent to "xs!.@% !n5 must be nonnegative.@@@@$4.14@@@0Invalid_argument#if !n- is negative.@@@@@b@ c@ _@@@@I(Seq.drop3)drop n xs1 is the sequence "xs8, deprived of its first !n* elements.@( If "xs0 has fewer than !n4 elements, then )drop n xs* is empty.@% !n5 must be nonnegative.@% $drop4 is lazy: the first #n+1: elements of the sequence "xs 1 are demanded only when the first element of )drop n xs1 is demanded.@@@@$4.14@@@0Invalid_argument#if !n- is negative.@@@@@@ {@ ~@@@@.Seq.take_while3/take_while p xs ' is the longest prefix of the sequence "xs9 where every element !x+ satisfies #p x!.@@@@$4.14@@@@@@@@ @ @@@@.Seq.drop_while3/drop_while p xs1 is the sequence "xs=, deprived of the prefix /take_while p xs!.@@@@$4.14@@@@@@@@ @ @@@@~)Seq.group36Provided the function "eq & defines an equality on elements, +group eq xs X is the sequence of the maximal runs of adjacent duplicate elements of the sequence "xs!.@6 Every element of +group eq xs * is a nonempty sequence of equal elements.@7 The concatenation 4concat (group eq xs)- is equal to "xs!.@/ Consuming +group eq xs >, and consuming the sequences that it contains, can cause "xs + to be consumed more than once. Therefore, "xs8 must be persistent.@@@@$4.14@@@@@@@@ @ @@@@+Seq.memoize3-The sequence *memoize xs ' has the same elements as the sequence "xs!.@; Regardless of whether "xs ! is ephemeral or persistent, *memoize xs 9 is persistent: even if it is queried several times, "xs9 is queried at most once.@ & The construction of the sequence *memoize xs = internally relies on suspensions provided by the module $Lazy@@<. These suspensions are #not@ & thread-safe. Therefore, the sequence *memoize xs* must #not@ - be queried by multiple threads concurrently.@@@@$4.14@@@@@@@@ y@@@@Ӡ0Seq.Forced_twice3 5This exception is raised when a sequence returned by (Seq.onceD@ 3 (or a suffix of it) is queried more than once.@@@@$4.14@@@@@@@@@@@@@3-The sequence 'once xs ' has the same elements as the sequence "xs!.@; Regardless of whether "xs ! is ephemeral or persistent, 'once xs is an ephemeral sequence: it can be queried at most once. If it (or a suffix of it) is queried more than once, then the exception ,Forced_twice s is raised. This can be useful, while debugging or testing, to ensure that a sequence is consumed at most once.@@@@$4.14@@@,Forced_twice#if 'once xs :, or a suffix of it, is queried more than once.@@@@@ @ @@@@-Seq.transpose3#If #xss ( is a matrix (a sequence of rows), then -transpose xss 2 is the sequence of the columns of the matrix #xss!.@< The rows of the matrix #xss * are not required to have the same length.@0 The matrix #xss 4 is not required to be finite (in either direction).@0 The matrix #xss4 must be persistent.@@@@$4.14@@@@@@@+@ ,@@@@A@3Combining sequences@@*Seq.append3,append xs ys ' is the concatenation of the sequences "xs% and "ys!.@ & Its elements are the elements of "xs>, followed by the elements of "ys!.@@@@$4.11@@@@@@@-@ F.@ I @@@@*Seq.concat3#If #xss & is a sequence of sequences, then *concat xss6 is its concatenation.@( If #xss1 is the sequence -xs0; xs1; ...* then *concat xss1 is the sequence /xs0 @ xs1 @ ...!.@@@@$4.13@@@@@@@;@ <@@@@,Seq.flat_map3-flat_map f xs2 is equivalent to 1concat (map f xs)!.@@@@@@@@@@@@'@ (@ @@@@.Seq.concat_map3/concat_map f xs2 is equivalent to 1concat (map f xs)!.@% *concat_map1 is an alias for (flat_map!.@@@@$4.13@@@@@@@@ @  @@@@'Seq.zip3)zip xs ys: is the sequence of pairs &(x, y) , drawn synchronously from the sequences "xs% and "ys!.@6 If the sequences "xs% and "ys have different lengths, then the sequence ends as soon as one sequence is exhausted; the excess elements in the other sequence are ignored.@% )zip xs ys2 is equivalent to >map2 (fun a b -> (a, b)) xs ys!.@@@@$4.14@@@@@@@1@ 2@ $@@@@(Seq.map23,map2 f xs ys ! is the sequence of the elements %f x y6, where the pairs &(x, y) 0 are drawn synchronously from the sequences "xs% and "ys!.@6 If the sequences "xs% and "ys have different lengths, then the sequence ends as soon as one sequence is exhausted; the excess elements in the other sequence are ignored.@% ,map2 f xs ys2 is equivalent to %map (fun (x, y) -> f x y) (zip xs ys)!.@@@@$4.14@@@@@@@F@ _G@ b2@ e*@@@@.Seq.interleave30interleave xs ys ; is the sequence that begins with the first element of "xs &, continues with the first element of "ys,, and so on.@? When one of the sequences "xs% and "ys3 is exhausted, 0interleave xs ys / continues with the rest of the other sequence.@@@@$4.14@@@@@@@B@ C@ 5@@@@%0Seq.sorted_merge31If the sequences "xs% and "ys 0 are sorted according to the total preorder #cmp', then 6sorted_merge cmp xs ys > is the sorted sequence obtained by merging the sequences "xs% and "ys!.@ 3 For more details on comparison functions, see *Array.sort@@!.@@@@$4.14@@@@@@@V@ W@ I@ A@@@@1+Seq.product3-product xs ys + is the Cartesian product of the sequences "xs% and "ys!.@7 For every element !x$ of "xs7 and for every element !y$ of "ys/, the pair &(x, y)? appears once as an element of -product xs ys!.@ 8 The order in which the pairs appear is unspecified.@3 The sequences "xs% and "ys? are not required to be finite.@3 The sequences "xs% and "ys4 must be persistent.@@@@$4.14@@@@@@@@ O@ R@@@@f/Seq.map_product3-The sequence 3map_product f xs ys6 is the image through !f / of the Cartesian product of the sequences "xs% and "ys!.@7 For every element !x$ of "xs7 and for every element !y$ of "ys2, the element %f x y? appears once as an element of 3map_product f xs ys!.@ = The order in which these elements appear is unspecified.@3 The sequences "xs% and "ys? are not required to be finite.@3 The sequences "xs% and "ys4 must be persistent.@% 3map_product f xs ys6 is equivalent to )map (fun (x, y) -> f x y) (product xs ys)!.@@@@$4.14@@@@@@@@ @ @ @@@@A@ 'Splitting a sequence into two sequences@@)Seq.unzip3%unzip 9 transforms a sequence of pairs into a pair of sequences.@% (unzip xs2 is equivalent to 8(map fst xs, map snd xs)!.@ 2 Querying either of the sequences returned by (unzip xs, causes "xs @ to be queried. Therefore, querying both of them causes "xs to be queried twice. Thus, "xs @ must be persistent and cheap. If that is not the case, use 2unzip (memoize xs)!.@@@@$4.14@@@@@@@@*@@@@)Seq.split3%split1 is an alias for %unzip!.@@@@$4.14@@@@@@@@D@@@@1Seq.partition_map32partition_map f xs= returns a pair of sequences ((ys, zs)(, where:@"ys ! is the sequence of the elements !y1 such that ,f x = Left y(, where !x- ranges over "xs!;@@"zs ! is the sequence of the elements !z1 such that -f x = Right z(, where !x- ranges over "xs!.@@% 2partition_map f xs is equivalent to a pair of &filter_map Either.find_left (map f xs)) and 'filter_map Either.find_right (map f xs)!.@ 2 Querying either of the sequences returned by 2partition_map f xs, causes "xs @ to be queried. Therefore, querying both of them causes "xs to be queried twice. Thus, "xs @ must be persistent and cheap. If that is not the case, use not (p x)) xs!.@ 1 Consuming both of the sequences returned by .partition p xs, causes "xs . to be consumed twice and causes the function !f E to be applied twice to each element of the list. Therefore, !f , should be pure and cheap. Furthermore, "xs B should be persistent and cheap. If that is not the case, use 8partition p (memoize xs)!.@@@@$4.14@@@@@@@;@?<@B0@@@@A@ +Converting between sequences and dispensers@@ HA dispenser is a representation of a sequence as a function of type 1unit -> 'a option . Every time this function is invoked, it returns the next element of the sequence. When there are no more elements, it returns $None . A dispenser has mutable internal state, therefore is ephemeral: the sequence that it represents can be consumed at most once.@m3/of_dispenser it ? is the sequence of the elements produced by the dispenser "it p. It is an ephemeral sequence: it can be consumed at most once. If a persistent sequence is needed, use 9memoize (of_dispenser it)!.@@@@$4.14@@@@@@@:@y;@@@@"3/to_dispenser xs & is a fresh dispenser on the sequence "xs!.@ This dispenser has mutable internal state, which is not protected by a lock; so, it must not be used by several threads concurrently.@@@@$4.14@@@@@@@1@2@$@@@@A@5Sequences of integers@@(Seq.ints3&ints i 7 is the infinite sequence of the integers beginning at !i5 and counting up.@@@@$4.14@@@@@@@/@0@@@@@@@@A&Either@@@@@