123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373(* This file is free software, part of gen. See file "license" for more details. *)(** {1 Common signature for transient and restartable generators}
The signature {!S} abstracts on a type ['a t], where the [t] can be
the type of transient or restartable generators. Some functions specify
explicitly that they use ['a gen] (transient generators). *)type'agen=unit->'aoptiontype'aiter=('a->unit)->unitmoduletypeS=sigtype'atvalempty:'at(** Empty generator, with no elements *)valsingleton:'a->'at(** One-element generator *)valreturn:'a->'at(** Alias to {!singleton}
@since 0.3 *)valrepeat:'a->'at(** Repeat same element endlessly *)valiterate:'a->('a->'a)->'at(** [iterate x f] is [[x; f x; f (f x); f (f (f x)); ...]] *)valunfold:('b->('a*'b)option)->'b->'at(** Dual of {!fold}, with a deconstructing operation. It keeps on
unfolding the ['b] value into a new ['b], and a ['a] which is yielded,
until [None] is returned. *)valinit:?limit:int->(int->'a)->'at(** Calls the function, starting from 0, on increasing indices.
If [limit] is provided and is a positive int, iteration will
stop at the limit (excluded).
For instance [init ~limit:4 id] will yield 0, 1, 2, and 3. *)(** {2 Basic combinators}
{b Note}: those combinators, applied to generators (not restartable
generators) {i consume} their argument. Sometimes they consume it lazily,
sometimes eagerly, but in any case once [f gen] has been called (with [f] a
combinator), [gen] shouldn't be used anymore. *)valis_empty:_t->bool(** Check whether the gen is empty. Pops an element, if any *)valfold:('b->'a->'b)->'b->'at->'b(** Fold on the generator, tail-recursively. Consumes the generator. *)valreduce:('a->'a->'a)->'at->'a(** Fold on non-empty sequences. Consumes the generator.
@raise Invalid_argument on an empty gen *)valscan:('b->'a->'b)->'b->'at->'bt(** Like {!fold}, but keeping successive values of the accumulator.
Consumes the generator. *)valunfold_scan:('b->'a->'b*'c)->'b->'at->'ct(** A mix of {!unfold} and {!scan}. The current state is combined with
the current element to produce a new state, and an output value
of type 'c.
@since 0.2.2 *)valiter:('a->unit)->'at->unit(** Iterate on the gen, consumes it. *)valiteri:(int->'a->unit)->'at->unit(** Iterate on elements with their index in the gen, from 0, consuming it. *)vallength:_t->int(** Length of an gen (linear time), consuming it *)valmap:('a->'b)->'at->'bt(** Lazy map. No iteration is performed now, the function will be called
when the result is traversed. *)valmapi:(int->'a->'b)->'at->'bt(** Lazy map with indexing starting from 0. No iteration is performed now,
the function will be called when the result is traversed.
@since 0.5 *)valfold_map:('b->'a->'b)->'b->'at->'bt(** Lazy fold and map. No iteration is performed now, the function will be
called when the result is traversed. The result is
an iterator over the successive states of the fold.
@since 0.2.4 *)valappend:'at->'at->'at(** Append the two gens; the result contains the elements of the first,
then the elements of the second gen. *)valflatten:'agent->'at(** Flatten the generator of generators *)valflat_map:('a->'bgen)->'at->'bt(** Monadic bind; each element is transformed to a sub-gen
which is then iterated on, before the next element is processed,
and so on. *)valmem:?eq:('a->'a->bool)->'a->'at->bool(** Is the given element, member of the gen? *)valtake:int->'at->'at(** Take at most n elements *)valdrop:int->'at->'at(** Drop n elements *)valnth:int->'at->'a(** n-th element, or Not_found
@raise Not_found if the generator contains less than [n] arguments *)valtake_nth:int->'at->'at(** [take_nth n g] returns every element of [g] whose index
is a multiple of [n]. For instance [take_nth 2 (1--10) |> to_list]
will return [1;3;5;7;9] *)valfilter:('a->bool)->'at->'at(** Filter out elements that do not satisfy the predicate. *)valtake_while:('a->bool)->'at->'at(** Take elements while they satisfy the predicate. The initial generator
itself is not to be used anymore after this. *)valfold_while:('a->'b->'a*[`Stop|`Continue])->'a->'bt->'a(** Fold elements until (['a, `Stop]) is indicated by the accumulator.
@since 0.2.4 *)valdrop_while:('a->bool)->'at->'at(** Drop elements while they satisfy the predicate. The initial generator
itself should not be used anymore, only the result of [drop_while]. *)valfilter_map:('a->'boption)->'at->'bt(** Maps some elements to 'b, drop the other ones *)valzip_index:'at->(int*'a)t(** Zip elements with their index in the gen *)valunzip:('a*'b)t->'at*'bt(** Unzip into two sequences, splitting each pair *)valpartition:('a->bool)->'at->'at*'at(** [partition p l] returns the elements that satisfy [p],
and the elements that do not satisfy [p] *)valfor_all:('a->bool)->'at->bool(** Is the predicate true for all elements? *)valexists:('a->bool)->'at->bool(** Is the predicate true for at least one element? *)valmin:?lt:('a->'a->bool)->'at->'a(** Minimum element, according to the given comparison function.
@raise Invalid_argument if the generator is empty *)valmax:?lt:('a->'a->bool)->'at->'a(** Maximum element, see {!min}
@raise Invalid_argument if the generator is empty *)valeq:?eq:('a->'a->bool)->'at->'at->bool(** Equality of generators. *)vallexico:?cmp:('a->'a->int)->'at->'at->int(** Lexicographic comparison of generators. If a generator is a prefix
of the other one, it is considered smaller. *)valcompare:?cmp:('a->'a->int)->'at->'at->int(** Synonym for {! lexico} *)valfind:('a->bool)->'at->'aoption(** [find p e] returns the first element of [e] to satisfy [p],
or None. *)valsum:intt->int(** Sum of all elements *)(** {2 Multiple iterators} *)valmap2:('a->'b->'c)->'at->'bt->'ct(** Map on the two sequences. Stops once one of them is exhausted.*)valiter2:('a->'b->unit)->'at->'bt->unit(** Iterate on the two sequences. Stops once one of them is exhausted.*)valfold2:('acc->'a->'b->'acc)->'acc->'at->'bt->'acc(** Fold the common prefix of the two iterators *)valfor_all2:('a->'b->bool)->'at->'bt->bool(** Succeeds if all pairs of elements satisfy the predicate.
Ignores elements of an iterator if the other runs dry. *)valexists2:('a->'b->bool)->'at->'bt->bool(** Succeeds if some pair of elements satisfy the predicate.
Ignores elements of an iterator if the other runs dry. *)valzip_with:('a->'b->'c)->'at->'bt->'ct(** Combine common part of the gens (stops when one is exhausted) *)valzip:'at->'bt->('a*'b)t(** Zip together the common part of the gens *)(** {2 Complex combinators} *)valmerge:'agent->'at(** Pick elements fairly in each sub-generator. The merge of gens
[e1, e2, ... ] picks elements in [e1], [e2],
in [e3], [e1], [e2] .... Once a generator is empty, it is skipped;
when they are all empty, and none remains in the input,
their merge is also empty.
For instance, [merge [1;3;5] [2;4;6]] will be, in disorder, [1;2;3;4;5;6]. *)valintersection:?cmp:('a->'a->int)->'at->'at->'at(** Intersection of two sorted sequences. Only elements that occur in both
inputs appear in the output *)valsorted_merge:?cmp:('a->'a->int)->'at->'at->'at(** Merge two sorted sequences into a sorted sequence *)valsorted_merge_n:?cmp:('a->'a->int)->'atlist->'at(** Sorted merge of multiple sorted sequences *)valtee:?n:int->'at->'agenlist(** Duplicate the gen into [n] generators (default 2). The generators
share the same underlying instance of the gen, so the optimal case is
when they are consumed evenly *)valround_robin:?n:int->'at->'agenlist(** Split the gen into [n] generators in a fair way. Elements with
[index = k mod n] with go to the k-th gen. [n] default value
is 2. *)valinterleave:'at->'at->'at(** [interleave a b] yields an element of [a], then an element of [b],
and so on. When a generator is exhausted, this behaves like the
other generator. *)valintersperse:'a->'at->'at(** Put the separator element between all elements of the given gen *)valproduct:'at->'bt->('a*'b)t(** Cartesian product, in no predictable order. Works even if some of the
arguments are infinite. *)valgroup:?eq:('a->'a->bool)->'at->'alistt(** Group equal consecutive elements together. *)valuniq:?eq:('a->'a->bool)->'at->'at(** Remove consecutive duplicate elements. Basically this is
like [fun e -> map List.hd (group e)]. *)valsort:?cmp:('a->'a->int)->'at->'at(** Sort according to the given comparison function. The gen must be finite. *)valsort_uniq:?cmp:('a->'a->int)->'at->'at(** Sort and remove duplicates. The gen must be finite. *)valchunks:int->'at->'aarrayt(** [chunks n e] returns a generator of arrays of length [n], composed
of successive elements of [e]. The last array may be smaller
than [n] *)valpermutations:'at->'alistt(** Permutations of the gen.
@since 0.2.2 *)valpermutations_heap:'at->'aarrayt(** Permutations of the gen, using Heap's algorithm.
@since 0.2.3 *)valcombinations:int->'at->'alistt(** Combinations of given length. The ordering of the elements within
each combination is unspecified.
Example (ignoring ordering):
[combinations 2 (1--3) |> to_list = [[1;2]; [1;3]; [2;3]]]
@since 0.2.2 *)valpower_set:'at->'alistt(** All subsets of the gen (in no particular order). The ordering of
the elements within each subset is unspecified.
@since 0.2.2 *)(** {2 Basic conversion functions} *)valof_list:'alist->'at(** Enumerate elements of the list *)valto_list:'at->'alist(** non tail-call trasnformation to list, in the same order *)valto_rev_list:'at->'alist(** Tail call conversion to list, in reverse order (more efficient) *)valto_array:'at->'aarray(** Convert the gen to an array (not very efficient) *)valof_array:?start:int->?len:int->'aarray->'at(** Iterate on (a slice of) the given array *)valof_string:?start:int->?len:int->string->chart(** Iterate on bytes of the string *)valto_string:chart->string(** Convert into a string *)valto_buffer:Buffer.t->chart->unit(** Consumes the iterator and writes to the buffer *)valrand_int:int->intt(** Random ints in the given range. *)valint_range:?step:int->int->int->intt(** [int_range ~step a b] generates integers between [a] and [b], included,
with steps of length [step] (1 if omitted). [a] is assumed to be smaller
than [b], otherwise the result will be empty.
@raise Invalid_argument if [step=0]
@param step step between two numbers; must not be zero,
but it can be negative for decreasing ranges (@since 0.5). *)vallines:chart->stringt(** Group together chars belonging to the same line
@since 0.3 *)valunlines:stringt->chart(** Explode lines into their chars, adding a ['\n'] after each one
@since 0.3 *)moduleInfix:sigval(--):int->int->intt(** Synonym for {! int_range ~by:1} *)val(>>=):'at->('a->'bgen)->'bt(** Monadic bind operator *)val(>>|):'at->('a->'b)->'bt(** Infix map operator
@since 0.2.3 *)val(>|=):'at->('a->'b)->'bt(** Infix map operator
@since 0.2.3 *)endval(--):int->int->intt(** Synonym for {! int_range ~by:1} *)val(>>=):'at->('a->'bgen)->'bt(** Monadic bind operator *)val(>>|):'at->('a->'b)->'bt(** Infix map operator
@since 0.2.3 *)val(>|=):'at->('a->'b)->'bt(** Infix map operator
@since 0.2.3 *)valpp:?start:string->?stop:string->?sep:string->?horizontal:bool->(Format.formatter->'a->unit)->Format.formatter->'at->unit(** Pretty print the content of the generator on a formatter. *)valof_seq:'aSeq.t->'at(** @since 1.0 *)valto_iter:'at->'aiter(** @since 1.0 *)end