BatteriesExceptionless.LazyListSourceinclude module type of Batteries.LazyList
with module Labels := Batteries.LazyList.LabelsEmpty_list is raised when an operation applied on an empty list is invalid. For instance, hd nil will raise Empty_list.
Invalid_index is raised when an indexed access on a list is out of list bounds.
Different_list_size is raised when applying functions such as iter2 on two lists having different size.
Note The types are kept concrete so as to allow pattern-matching. However, it is generally easier to manipulate nil and cons.
and 'a node_t = 'a BatLazyList.node_t = | Nil| Cons of 'a * 'a tThe type of an item in the list.
*)include BatEnum.Enumerable with type 'a enumerable = 'a tinclude BatInterfaces.Mappable with type 'a mappable = 'a tfrom next creates a (possibly infinite) lazy list from the successive results of next.
from next creates a (possibly infinite) lazy list from the successive results of next. The list ends whenever next returns None.
seq data next cond creates a lazy list from the successive results of applying next to data, then to the result, etc. The list continues until the condition cond fails. For example, seq 1 ((+) 1) ((>) 100) returns [^1, 2, ... 99^]. If cond init is false, the result is empty. To create an infinite lazy list, pass (fun _ -> true) as cond.
unfold data next creates a (possibly infinite) lazy list from the successive results of applying next to data, then to the result, etc. The list ends whenever next returns None. The function next should return a pair option whose first element will be the current value of the sequence; the second element will be passed (lazily) to next in order to compute the following element. One example of a use of unfold is to make each element of the resulting sequence to depend on the previous two elements, as in this Fibonacci sequence definition:
let data = (1, 1)
let next (x, y) = Some (x, (y, x + y))
let fib = unfold data nextThe first element x of the pair within Some will be the current value of the sequence; the next value of the sequence, and the one after that, are recorded as y and x + y respectively.
from_loop data next creates a (possibly infinite) lazy list from the successive results of applying next to data, then to the result, etc. The list ends whenever the function raises LazyList.No_more_elements. (For further information see unfold; ignore references to option and Some.)
Similar to Array.init, init n f returns the lazy list containing the results of (f 0),(f 1).... (f (n-1)).
Similar to String.make, make n x returns a list containing n elements x.
Compute lazily a range of integers a .. b as a lazy list.
The range is empty if b <= a.
Eager iteration
iter f [^ a0; a1; ...; an ^] applies function f in turn to a0; a1; ...; an. It is equivalent to begin f a0; f a1; ...; f an; () end. In particular, it causes all the elements of the list to be evaluated.
Eager iteration, with indices
iteri f [^ a0; a1; ...; an ^] applies function f in turn to a0; a1;...; an, along with the corresponding 0,1..n index. It is equivalent to begin f 0 a0; f 1 a1; ...; f n an; () end. In particular, it causes all the elements of the list to be evaluated.
Lazy map
map f [^ a0; a1; ... ^] builds the list [^ f a0; f a1; ... ^] with the results returned by f. Not tail-recursive. Evaluations of f take place only when the contents of the list are forced.
Lazy map, with indices
mapi f [^ a0; a1; ... ^] builds the list [^ f 0 a0; f 1 a1; ... ^] with the results returned by f. Not tail-recursive. Evaluations of f take place only when the contents of the list are forced.
Eager fold_left
LazyList.fold_left f a [^ b0; b1; ...; bn ^] is f (... (f (f a b0) b1) ...) bn. This causes evaluation of all the elements of the list.
Eager fold_right
fold_right f b [^ a0; a1; ...; an ^] is f a0 (f a1 (... (f an b) ...)). This causes evaluation of all the elements of the list. Not tail-recursive.
Note that the argument order of this function is the same as fold_left above, but inconsistent with other fold_right functions in Batteries. We hope to fix this inconsistency in the next compatibility-breaking release, so you should rather use the more consistent eager_fold_right.
Eager fold_right
As fold_right above, but with the usual argument order for a fold_right.
Just as fold_left on a structure 'a t turns an element-level function of type ('b -> 'a -> 'b), with the accumulator argument 'b on the left, into a structure-level function 'b -> 'a t -> 'b, fold_right turns a function ('a -> 'b -> 'b) (accumulator on the right) into a 'a t -> 'b -> 'b.
Lazy fold_right lazy_fold_right f (Cons (a0, Cons (a1, Cons (a2, nil)))) b is lazy (f a0 (lazy (f a1 (lazy (f a2 b))))).
Forcing the result of lazy_fold_right forces the first element of the list; the rest is forced only if/when the function f forces its accumulator argument.
mem x l determines if x is part of l. Evaluates all the elements of l which appear before x.
find_exn p e l returns the first element of l such as p x returns true or raises e if such an element has not been found.
rfind_exn p e l returns the last element of l such as p x returns true or raises e if such an element has not been found.
index_of e l returns the index of the first occurrence of e in l, or None if there is no occurrence of e in l
index_ofq e l behaves as index_of e l except it uses physical equality
index_of e l returns the index of the last occurrence of e in l, or None if there is no occurrence of e in l
rindex_ofq e l behaves as rindex_of e l except it uses physical equality
Compute and return the first node from the list as a Cons. This differs from hd, which returns the first element (the first component of the first node).
Return the length (number of elements) of the given list.
Causes the evaluation of all the elements of the list.
would_at_fail l n returns true if l contains strictly less than n elements, false otherwise
Return the first element of the given list.
Note: this function does not comply with the usual exceptionless error-management recommendations, as doing so would essentially render it useless.
Return the given list without its first element.
Note: this function does not comply with the usual exceptionless error-management recommendations, as doing so would essentially render it useless.
Returns the last element of the list.
This function takes linear time and causes the evaluation of all elements of the list
These lists behave essentially as HashMap, although they are typically faster for short number of associations, and much slower for for large number of associations.
As assoc but simply returns true if a binding exists, false otherwise.
Evaluate a list and append another list after this one.
Cost is linear in the length of the first list, not tail-recursive.
Eager reverse-and-append
Cost is linear in the length of the first list, tail-recursive.
Lazy append
Cost is constant. All evaluation is delayed until the contents of the list are actually read. Reading itself is delayed by a constant.
unique cmp l returns the list l without any duplicate element. Default comparator ( = ) is used if no comparison function specified.
as unique except only uses an equality function. Use for short lists when comparing is expensive compared to equality testing
remove l x returns the list l without the first element x found or returns l if no element is equal to x. Elements are compared using ( = ).
remove_if cmp l is similar to remove, but with cmp used instead of ( = ).
remove_all l x is similar to remove but removes all elements that are equal to x and not only the first one.
remove_all_such f l is similar to remove but removes all elements that satisfy the predicate f and not only the first one.
take n l returns up to the n first elements from list l, if available.
drop n l returns l without the first n elements, or the empty list if l have less than n elements.
take_while f xs returns the first elements of list xs which satisfy the predicate f.
drop_while f xs returns the list xs with the first elements satisfying the predicate f dropped.
combinations l yields a list of all combinations of elements of l. Each combination selects a "subset" of the elements of l (duplicates are considered as distinct elements).
permutations l yields a lazy list of all permutations of the list l. Every permutation has the same elements as l, but in a different order. There are factorial (length l) permutations.
Lazy conversion from lists
Albeit slower than eager conversion, this is the default mechanism for converting from regular lists to lazy lists. This for two reasons : * if you're using lazy lists, total speed probably isn't as much an issue as start-up speed * this will let you convert regular infinite lists to lazy lists.
Eager conversion from lists.
This function is much faster than of_list but will freeze on cyclic lists.
Lazy filtering.
filter p l returns all the elements of the list l that satisfy the predicate p. The order of the elements in the input list is preserved.
Eager existential.
exists p [^ a0; a1; ... ^] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a0) || (p a1) || ... .
Eager universal.
for_all p [^ a0; a1; ... ^] checks if all elements of the list satisfy the predicate p. That is, it returns (p a0) && (p a1) && ... .
Lazily eliminate some elements and transform others.
filter_map f [^ a0; a1; ... ^] applies lazily f to each a0, a1... If f ai evaluates to None, the element is not included in the result. Otherwise, if f ai evaluates to Some x, element x is included in the result.
This is equivalent to match f a0 with | Some x0 -> x0 ^:^ (match f a1 with | Some x1 -> x1 ^:^ ... | None -> [^ ^]) | None -> [^ ^] .
Sort the list using optional comparator (by default compare).
map2 f [^ a0; a1; ...^] [^ b0; b1; ... ^] is [^ f a0 b0; f a1 b1; ... ^].
iter2 f [^ a0; ...; an ^] [^ b0; ...; bn ^] calls in turn f a0 b0; ...; f an bn. Tail-recursive, eager.
fold_left2 f a [^ b0; b1; ...; bn ^] [^ c0; c1; ...; cn ^] is f (... (f (f a b0 c0) b1 c1) ...) bn cn. Eager.
fold_right2 f [^ a0; a1; ...; an ^] [^ b0; b1; ...; bn ^] c is f a0 b0 (f a1 b1 (... (f an bn c) ...)). Eager.
Same as for_all, but for a two-argument predicate.
equal eq s1 s2 compares elements of s1 and s2 pairwise using eq and returns true if all elements pass the test and the lists have the same length; otherwise it returns false. Examples:
equal (=) (range 0 4) (range 0 4) (* true *)
(* Make lazy lists of lazy lists: *)
let s1 = init 5 (range 0)
let s2 = init 5 (range 0)
equal (equal (=)) s1 s2 (* true *)(Calling = directly on a pair of lazy lists may succeed but is not guaranteed to behave consistently.)
Note that on lists of equal length, equal and for_all2 can perform the same function; their intended uses differ, however, as signaled by behavior on lists of different lengths.
Same as exists, but for a two-argument predicate.
Transform a pair of lists into a list of pairs: combine [^ a0; a1; ... ^] [^ b0; b1; ... ^] is [^ (a0, b0); (a1, b1); ... ^].
val print :
?first:string ->
?last:string ->
?sep:string ->
('a BatInnerIO.output -> 'b -> unit) ->
'a BatInnerIO.output ->
'b t ->
unitThe following modules replace functions defined in LazyList with functions behaving slightly differently but having the same name. This is by design: the functions meant to override the corresponding functions of LazyList.
Exceptionless counterparts for error-raising operations
include module type of struct include BatLazyList.Exceptionless endfind p l returns Some x where x is the first element of l such that p x returns true or None if such element as not been found.
rfind p l returns Some x where x is the last element of l such that p x returns true or None if such element as not been found.
findi p e l returns Some (i, ai) where ai and i are respectively the first element of l and its index, such that p i ai is true, or None if no such element has been found.
rfindi p e l returns Some (i, ai) where ai and i are respectively the last element of l and its index, such that p i ai is true, or None if no such element has been found.
val split_at :
int ->
'a BatLazyList.t ->
[ `Ok of 'a BatLazyList.t * 'a BatLazyList.t | `Invalid_index of int ]Whenever n is inside of l size bounds, split_at n l returns `Ok (l1,l2), where l1 contains the first n elements of l and l2 contains the others. Otherwise, returns `Invalid_index n
If n is inside the bounds of l, at l n returns `Ok x, where x is the n-th element of the list l. Otherwise, returns `Invalid_index n.
assoc a l returns Some b where b is the value associated with key a in the list of pairs l. That is, assoc a [ ...; (a,b); ...] = Some b if (a,b) is the leftmost binding of a in list l. Return None if there is no value associated with a in the list l.
As assoc but with physical equality