Sequence that calls the given function to produce elements. The sequence may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
Cycle forever through the given sequence. Assume the given sequence can be traversed any amount of times (not transient). This yields an infinite sequence, you should use something like take not to loop forever.
val foldi : f:('a->int ->'b->'a)->init:'a->'bt->'a
Fold over elements of the sequence and their index, consuming it
val fold_map : f:('acc->'a->'acc * 'b)->init:'acc->'at->'bt
fold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
since 0.9
val fold_filter_map :
f:('acc->'a->'acc * 'b option)->init:'acc->'at->'bt
fold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
Iterate on the sequence, storing elements in an efficient internal structure.. The resulting sequence can be iterated on as many times as needed. Note: calling persistent on an already persistent sequence will still make a new copy of the sequence!
Lazy version of persistent. When calling persistent_lazy s, a new sequence s' is immediately returned (without actually consuming s) in constant time; the first time s' is iterated on, it also consumes s and caches its content into a inner data structure that will back s' for future iterations.
warning: on the first traversal of s', if the traversal is interrupted prematurely (take, etc.) then s' will not be memorized, and the next call to s' will traverse s again.
Checks whether the sequence is sorted. Eager, same as sort.
since 0.9
val group_succ_by : ?eq:('a->'a-> bool)->'at->'a listt
Group equal consecutive elements. Formerly synonym to group.
since 0.6
val group_by : ?hash:('a-> int)->?eq:('a->'a-> bool)->'at->'a listt
Group equal elements, disregarding their order of appearance. The result sequence is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
since 0.6
val count : ?hash:('a-> int)->?eq:('a->'a-> bool)->'at->('a * int)t
Map each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l)
Cartesian product of the sequences. When calling product a b, the caller MUST ensure that b can be traversed as many times as required (several times), possibly by calling persistent on it beforehand.
val join : join_row:('a->'b->'c option)->'at->'bt->'ct
join ~join_row a b combines every element of a with every element of b using join_row. If join_row returns None, then the two elements do not combine. Assume that b allows for multiple iterations.
val join_by :
?eq:'keyequal->?hash:'keyhash->('a->'key)->('b->'key)->merge:('key->'a->'b->'c option)->'at->'bt->'ct
join key1 key2 ~merge is a binary operation that takes two sequences a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
since 0.10
val join_all_by :
?eq:'keyequal->?hash:'keyhash->('a->'key)->('b->'key)->merge:('key->'a list->'b list->'c option)->'at->'bt->'ct
join_all_by key1 key2 ~merge is a binary operation that takes two sequences a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
compute the list l1 of elements of a that map to k
compute the list l2 of elements of b that map to k
call merge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.
since 0.10
val group_join_by :
?eq:'aequal->?hash:'ahash->('b->'a)->'at->'bt->('a * 'b list)t
group_join_by key2 associates to every element x of the first sequence, all the elements y of the second sequence such that eq x (key y). Elements of the first sequences without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
since 0.10
val inter : ?eq:'aequal->?hash:'ahash->'at->'at->'at
Intersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
since 0.10
val union : ?eq:'aequal->?hash:'ahash->'at->'at->'at
Union of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
since 0.10
val diff : ?eq:'aequal->?hash:'ahash->'at->'at->'at
Set difference. Eager.
since 0.10
val subset : ?eq:'aequal->?hash:'ahash->'at->'at-> bool
subset a b returns true if all elements of a belong to b. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
Take elements while they satisfy the predicate, then stops iterating. Will work on an infinite sequence s if the predicate is false for at least one element of s.
val fold_while :
f:('a->'b->'a * [ `Stop | `Continue ])->init:'a->'bt->'a
Folds over elements of the sequence, stopping early if the accumulator returns ('a, `Stop)
Reverse the sequence. O(n) memory and time, needs the sequence to be finite. The result is persistent and does not depend on the input being repeatable.
Convert the sequence into a list. Preserves order of elements. This function is tail-recursive, but consumes 2*n memory. If order doesn't matter to you, consider to_rev_list.
Iterates on characters of the input (can block when one iterates over the sequence). If you need to iterate several times on this sequence, use persistent.
Iterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int ->start:int ->stop:int ->int t
int_range_by ~step ~start:i ~stop:j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing sequence.
Pretty print a sequence of 'a, using the given pretty printer to print each elements. An optional separator string can be provided.
val pp_buf :
?sep:string ->(Buffer.t->'a-> unit)->Buffer.t->'at->
unit
Print into a buffer
val to_string : ?sep:string ->('a-> string)->'at-> string
Print into a string
Basic IO
Very basic interface to manipulate files as sequence of chunks/lines. The sequences take care of opening and closing files properly; every time one iterates over a sequence, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines: