Module LazyStream

LazyStream is a data structure that is like Seq and Stream but more lazily evaluated, though calling a function with the same parameters multiple times will create different streams and will be evaluated separately, and more provided functions

All provided LazyStream functions lazily evaluate streams or provide lazily-evaluated streams where applicable unless otherwise explicitly mentioned

Copyright (C) 2020 Nikunj Chawla

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://www.gnu.org/licenses/.

type 'a t =
| Nil
| Cons of 'a * 'a t lazy_t

The type associated with LazyStream

Nil indicates the end of the stream as [] does for lists Cons is similar to :: for lists, though Cons has an eagerly-evaluated head and a lazily-evaluated tail

val empty : 'a t

The empty stream, which is equivalent to Nil

val is_empty : 'a t -> bool

is_empty returns true if the given stream is equivalent to Nil

parameter s

The stream to compare to Nil

returns

true if the stream is equivalent to Nil

val cons : 'a -> 'a t -> 'a t

cons concatenates el to the beginning of s

parameter el

The element to concatenate onto s

parameter s

The stream on which to concatenate the element

returns

The stream created from concatenating the given element on the given stream

val from : 'a -> ('a -> 'a) -> 'a t

from returns an infinite stream with the first element being start and all successive elements being the result of applying next to the previous element

parameter start

The starting element of the stream

parameter next

The function applied to each previous element of the stream to get the next element

returns

An infinite stream with the first element being start and each successive element being the result of applying next to the previous element

val from_int : int -> int -> int t

from_int returns an infinite stream of ints with the first element being start and each successive element being the previous element plus inc

parameter start

The starting integer of the stream

parameter inc

The increment added to each previous element to get the next element

returns

An infinite stream of ints with the first element being start and each successive element being the previous element plus inc

val from_char : char -> int -> char t

from_char returns an infinite stream of chars with the first element being start and each successive element being the character with the character code of the sum of the previous character code and inc

parameter start

The starting char of the stream

parameter inc

The increment added to each previous element's character code to get the code of the next element

returns

An infinite stream of chars with the first element being start and each successive element the character with the character code of the sum of the previous character code and inc

val range : 'a -> 'a -> ('a -> 'a) -> 'a t

range returns a stream with the first element being start, all successive elements being the result of applying next to the previous element, and the final element being final

parameter start

The starting element of the stream

parameter final

The ending element of the stream

parameter next

The function applied to each previous element of the stream to get the next element

returns

A stream with the first element being start, each successive element being the result of applying next to the previous element, and the final element being finish

val range_int : int -> int -> int -> int t

range_int returns a stream of ints with the first element being start, each successive element being the previous element plus inc, and the final element being finish

parameter start

The starting int of the stream

parameter final

The ending int of the stream

parameter inc

The increment added to each previous element to get the next element

returns

A stream of ints with the first element being start, each successive element being the previous element plus inc, and the final element being finish

val range_char : char -> char -> int -> char t

range_char returns a stream of chars with the first element being start, each successive element being the character with the character code of the sum of the previous character code and inc, and the final element being finish

parameter start

The starting char of the stream

parameter final

The ending char of the stream

parameter inc

The increment added to each previous element's character code to get the code of the next element

returns

A stream of ints with the first element being start, each successive element being character with the character code of the sum of the previous character code and inc, and the final element being finish

val filter : ('a -> bool) -> 'a t -> 'a t

filter returns a stream that contains all of the elements from the given stream that return true when inputted into filter_fn

parameter filter_fn

The function used for filtering the given stream

parameter s

The stream to filter

returns

A stream that contains all of the elements from the given stream that return true when inputted into filter_fn

val find_all : ('a -> bool) -> 'a t -> 'a t

find_all is an alias for filter

val append : 'a t -> 'a t -> 'a t

append concatenates two streams

parameter s1

The stream on the left side of the concatenation

parameter s2

The stream on the right side of the concatenation

returns

The concatenation of the given streams

val append_lazy : 'a t -> 'a t lazy_t -> 'a t

append_lazy concatenates a stream and a computationally-deferred stream, mainly used when it is desired that the head of the second lazy stream should not be computed until all of the elements from the first stream have been iterated through

parameter s1

The stream on the left side of the concatenation

parameter s2

The stream on the right side of the concatenation with its computation deferred

returns

The concatenation of the given stream and computationally-deferred stream

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

mapi is the same as map, but the function is applied to the index of the element as first argument (counting from 0) and the element itself as second argument

parameter map_fn

The mapping function to apply to each index-element pair

parameter s

The stream to map

returns

The stream created from mapping each index-element pair of the given stream

val map : ('a -> 'b) -> 'a t -> 'b t

map applies the function map_fn to each element of the given stream and builds a stream with the result of mapping each element using map_fn

parameter map_fn

The mapping function to apply to each element

parameter s

The stream to map

returns

The stream created from mapping each element of the given stream

val filter_map : ('a -> 'b option) -> 'a t -> 'b t

filter_map applies fm_fn to every element of s, filters out the None elements, and returns the stream of the arguments of the Some elements

parameter fm_fn

The mapping function to apply to each element to return an option

parameter s

The stream on which to apply filter_map

returns

The result of applying fm_map to every element of s and filtering out the None elements

val flat_map : ('a -> 'b t) -> 'a t -> 'b t

flat_map maps each element to a substream using fm_fn and then returns the stream resulting from concatenating the substreams, using append to concatenate the substreams

parameter fm_fn

The mapping function to apply to each element to get a substream

parameter s

The stream on which to apply flat_map

returns

The result of mapping each element to a substream using fm_fn and then concatenating each of the substreams

val flat_map_lazy : ('a -> 'b t) -> 'a t -> 'b t

flat_map_lazy maps each element to a substream using fm_fn and then returns the stream resulting from concatenating the substreams, using append_lazy to concatenate the substreams

parameter fm_fn

The mapping function to apply to each element to get a substream

parameter s

The stream on which to apply flat_map

returns

The result of mapping each element to a substream using fm_fn and then concatenating each of the substreams

val map_mult : ('c -> 'a -> 'b) list -> ('a -> 'c) -> 'a t -> 'b t

map_mult maps each element to a number of outputs, mapping each element using each function in map_fns and producing as many elements of output for every element of input as is the length of map_fns

Additionally, for each element taken as input, a value is computed using shared_fn that is passed into all of the mapping functions and can be used to prevent redundant computation among the mapping functions

Examples (using a list-like representation for streams):

map_mult [(fun _ x -> x); (fun _ x -> x + 1)] (Fun.const ()) [1; 1; ...] = [1; 2; 1; 2; ...]

map_mult [(fun sq _ -> sq); (fun sq x -> x + sq)] (fun x -> x * x) [1; 2; 3; ...] = [1; 2; 4; 6; 9; 12; ...]

parameter map_fns

The functions to apply to the shared element created from each element of s and the element of s to make elements in the output stream

parameter shared_fn

The function that produces the value that is shared among all of the functions in map_fns

parameter s

The stream on which to apply map_mult

returns

The result of mapping all of the functions in map_fns to each element in s, using the value produced from shared_fn as one of the inputs to each function in map_fns

val zip : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

zip returns a stream that is the result of mapping pairs of elements from two given streams to an element in the output stream, ending when one of the streams runs out of elements

parameter zip_fn

The function used to map each element pair from s1 and s2 to an element in the output stream

parameter s1

The stream whose elements will be used as the first argument of zip_fn

parameter s2

The stream whose elements will be used as the second argument of zip_fn

returns

The result of mapping each pair of elements from the two given streams to a new element in the output stream

val zip_long : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'a -> 'b -> 'c t

zip_long returns a stream that is the result of mapping pairs of elements from two given streams to an element in the output stream, substituting placeholders for elements in the stream that runs out first and ending when both streams have run out of elements

parameter zip_fn

The function used to map each element pair from s1 and s2 to an element in the output stream

parameter s1

The stream whose elements will be used as the first argument of zip_fn

parameter s2

The stream whose elements will be used as the second argument of zip_fn

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The result of mapping each pair of elements from the two given streams to a new element in the output stream

val hd : 'a t -> 'a

hd returns the first element of the given stream, raising Failure "hd" if there are no more elements in the given stream

parameter s

The stream from which to retrieve the head

returns

The first element of the given stream

raises Failure

Raised if there are no more elements in the given stream

val tl : 'a t -> 'a t

tl returns the given stream without its first element, raising Failure "tl" if there are no elements in the given stream

parameter s

The stream from which to retrieve the tail

returns

The given stream without its first element

raises Failure

Raised if there are no elements in the given stream

val take : int -> 'a t -> 'a list

take returns the first n elements of the given stream as a list, but if there are less then n elements in the stream, it returns the remaining elements from the stream

parameter n

The number of elements to retrieve from the given stream

parameter s

The stream from which to retrieve the elements

returns

The first n elements of the given stream as a list, but if there are less then n elements in the stream, it returns the remaining elements from the stream

val take_except : int -> 'a t -> 'a list

take_except returns the first n elements of the given stream as a list, but if there are less then n elements in the stream, it raises Failure "take_except"

parameter n

The number of elements to retrieve from the given stream

parameter s

The stream from which to retrieve the elements

returns

The first n elements of the given stream as a list

raises Failure

Raised if the given stream has less than n elements

val take_all : 'a t -> 'a list

take_all returns the given stream as a list

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s

The stream to convert to a list

returns

The given stream as a list

val drop : int -> 'a t -> 'a t

drop returns the given stream without its first n elements, but if there are less than n elements in the stream, then it simply returns Nil

parameter n

The number of elements to drop from the given stream

parameter s

The stream from which elements are dropped

returns

The given stream without its first n elements, but if there are less than n elements in the stream, then it simply returns Nil

val drop_except : int -> 'a t -> 'a t

drop_except returns the given stream without its first n elements, but if there are less than n elements in the stream, then it raises Failure "drop_except"

parameter n

The number of elements to drop from the given stream

parameter s

The stream from which elements are dropped

returns

The given stream without its first n elements

raises Failure

Raised if there are less than n elements

val to_seq : 'a t -> 'a Stdlib.Seq.t

to_seq converts the given stream to a Seq.t

parameter s

The stream to convert to a sequence

returns

s as a sequence

val of_seq : 'a Stdlib.Seq.t -> 'a t

of_seq converts the given Seq.t to a stream

parameter seq

The sequence to convert to a stream

returns

seq as a stream

val to_list : 'a t -> 'a list

to_list is an alias of take_all

val of_list : 'a list -> 'a t

of_list converts the given list to a stream

parameter lst

The list to convert to a stream

returns

lst as a stream

val to_string : char t -> string

to_string converts the given char stream to a string

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s

The char stream to convert to a string

returns

s as a string

val of_string : string -> char t

of_string converts the given string to a char stream

parameter str

The string to convert to a char stream

returns

str as a char stream

val to_bytes : char t -> bytes

to_bytes converts the given char stream to bytes

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s

The char stream to convert to bytes

returns

s as bytes

val of_bytes : bytes -> char t

of_string converts the given bytes to a char stream

parameter b

The bytes to convert to a char stream

returns

b as a char stream

val output_to_channel : Stdlib.out_channel -> char t -> unit

output_to_channel outputs each of the chars in the given char stream into the given out_channel

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter chan

The channel to output the chars

parameter s

The stream from which the chars are outputted

returns

unit

val of_channel : Stdlib.in_channel -> char t

of_channel lazily reads chars from the given in_channel and returns a char stream

parameter chan

The channel from which to read chars

returns

A stream for reading chars from the given channel

val repeat_elements : int -> 'a t -> 'a t

repeat_elements returns a stream with all of the given stream's elements each repeated n times in a row

parameter n

The number of times to repeat each element

parameter s

The stream that will have its elements repeated

returns

A stream with all of the given stream's elements each repeated n times in a row

val repeat_stream : int -> 'a t -> 'a t

repeat_stream returns a stream that essentially is the given stream repeated n times in a row

parameter n

The number of times to repeat the stream

parameter s

The stream that will have its elements repeated

returns

A stream with all of the given stream's elements each repeated n times in a row

val cartesian_product : 'a t -> 'b t -> ('a * 'b) t

cartesian_product Returns a stream representing the cartesian product of two streams

parameter s1

The stream whose elements will each be the outer (first) element in the cartesian product

parameter s2

The stream whose elements will each be the inner (second) element in the cartesian product

returns

A stream representing the cartesian product of two streams

val rev : 'a t -> 'a t

rev reverses the given stream

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s

The stream to reverse

returns

The reversal of the given stream

val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b

fold_left traverses the stream from left to right, combining each element with acc using fold_fn and returns the result of doing so

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter fold_fn

The function used to reduce the stream going from left to right

parameter acc

The accumulator used in gathering the result of reducing the given stream

parameter s

The stream to reduce from the left

returns

The result of applying fold_fn with the accumulator and the current element through the entire stream from left to right

val fold_left_lazy : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t

fold_left_lazy traverses the stream from left to right, combining each element with acc using fold_fn and returns each intermediary result in a stream from acc to the result of completely applying fold_left to the entire stream

parameter fold_fn

The function used to reduce the stream going from left to right

parameter acc

The accumulator used in gathering the result of reducing the given stream at each step

parameter s

The stream to reduce from the left

returns

A stream consisting of each intermediary value of applying fold_fn with the accumulator and the current element through the entire stream from left to right

val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b

fold_right traverses the stream from right to left, combining each element with acc using fold_fn and returns the result of doing so

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter fold_fn

The function used to reduce the stream going from right to left

parameter s

The stream to reduce from the right

parameter acc

The accumulator used in gathering the result of reducing the given stream

returns

The result of applying fold_fn with the accumulator and the current element through the entire stream from right to left

val length : 'a t -> int

length returns the number of elements in the given stream

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s

The stream whose length is returned

returns

The number of elements in the given stream

val compare_lengths : 'a t -> 'b t -> int

compare_lengths compares the lengths of the two given streams, and compare_lengths s1 s2 is equivalent to compare (length s1) (length s2), except that the computation stops after iteration completes on the shortest stream

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s1

The stream on the left side of the comparison

parameter s2

The stream on the right side of the comparison

returns

-1 if s1 has less elements than s2, 0 if s1 has an equal number of elements as s2, and 1 if s1 has a greater number of elements than s2

val nth_opt : 'a t -> int -> 'a option

nth_opt returns the nth element of the given stream wrapped in Some, but if the stream has less than n elements, then None is returned

parameter s

The stream from which to retrieve the nth element

parameter n

The element number to obtain

returns

The nth element of the given stream wrapped in Some, but if the stream has less than n elements, then None is returned

val nth : 'a t -> int -> 'a

nth returns the nth element of the given stream, but if the stream has less than n elements, then Invalid_arg "LazyStream.nth" is raised

parameter s

The stream from which to retrieve the nth element

parameter n

The element number to obtain

returns

The nth element of the given stream wrapped in Some

raises Invalid_arg

Raised if the stream has less than n elements

val init : int -> (int -> 'a) -> 'a t

init creates a stream whose elements are composed using calls to init_fn with the integers 0 through len - 1 inclusive, raising Invalid_arg "LazyStream.init" if len is negative

parameter len

The final length of the stream

parameter init_fn

The function used to map the integers 0 through len - 1 to values of this stream

returns

The stream whose elements are composed using calls to init_fn with the integers 0 through len - 1 inclusive

raises Invalid_arg

Raised if len is negative

val rev_append : 'a t -> 'a t -> 'a t

rev_append reverses s1 and concatenates it to s2, which is equivalent to append (rev s1) s2

This involves an immediate traversal over all of the elements ONLY for s1, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s1

The stream that will be reversed and then concatenated to s2

parameter s2

The stream onto which the reversal of s1 will be concatenate

returns

The stream created by reversing s1 and concatenating it to s2

val rev_append_lazy : 'a t -> 'a t lazy_t -> 'a t

rev_append reverses s1 and concatenates it to the computationally-delayed s2, making this function equivalent to append_lazy (rev s1) s2

This involves an immediate traversal over all of the elements ONLY for s1, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter s1

The stream that will be reversed and then concatenated to s2

parameter s2

The computationally-delayed stream onto which the reversal of s1 will be concatenate

returns

The stream created by reversing s1 and concatenating it to s2

val concat : 'a t t -> 'a t

concat concatenates a stream of streams, meaning that the elements of ss are all concatenated together in the same order, using append

parameter ss

The stream of streams to concatenate

returns

The concatenation of the streams that compose the given stream

val concat_lazy : 'a t t -> 'a t

concat_lazy concatenates a stream of streams, meaning that the elements of ss are all concatenated together in the same order, using append_lazy

parameter ss

The stream of streams to concatenate

returns

The concatenation of the streams that compose the given stream

val flatten : 'a t t -> 'a t

flatten is an alias of concat

val flatten_lazy : 'a t t -> 'a t

flatten_lazy is an alias of concat_lazy

val iteri : (int -> 'a -> unit) -> 'a t -> unit

iteri is the same as iter, but iter_fn is applied to the index of the element as the first argument and the element itself as the second argument

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter iter_fn

The iter function to apply to each index-element pair

parameter s

The stream over which to iterate

returns

Unit

val iter : ('a -> unit) -> 'a t -> unit

iter applies iter_fn to each element of the given stream

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter iter_fn

The iter function to apply to each element

parameter s

The stream over which to iterate

returns

Unit

val combine : 'a t -> 'b t -> ('a * 'b) t

combine transforms the pair of streams s1 and s2 into a stream of pairs, ending when one of the streams runs out of elements

parameter s1

The stream whose elements each compose the first element in each pair

parameter s2

The stream whose elements each compose the second element in each pair

returns

The stream of pairs composed from the pair of streams s1 and s2, ending when one of the streams runs out of elements

val combine_long : 'a t -> 'b t -> 'a -> 'b -> ('a * 'b) t

combine_long transforms the pair of streams s1 and s2 into a stream of pairs, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

parameter s1

The stream whose elements each compose the first element in each pair

parameter s2

The stream whose elements each compose the second element in each pair

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The stream of pairs composed from the pair of streams s1 and s2, using placeholder values when one of the streams runs out of elements, ending when both streams run out of values

val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit

iter2 applies the function iter_fn to each pair of elements from the given streams, ending when one of the streams runs out of elements

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter iter_fn

The iter function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is iterated over

parameter s2

The stream whose elements each compose the second element in each pair that is iterated over

returns

Unit

val iter2_long : ('a -> 'b -> unit) -> 'a t -> 'b t -> 'a -> 'b -> unit

iter2_long applies the function iter_fn to each pair of elements from the given streams, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter iter_fn

The iter function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is iterated over

parameter s2

The stream whose elements each compose the second element in each pair that is iterated over

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

Unit

val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

map2 applies the function map_fn to each pair of elements from the given streams and builds a stream with the result of mapping each pair of elements using map_fn, ending when one of the streams runs out of elements

parameter map_fn

The mapping function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

returns

The stream created from mapping each pair of elements from the given streams

val map2_long : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'a -> 'b -> 'c t

map2_long applies the function map_fn to each pair of elements from the given streams and builds a stream with the result of mapping each pair of elements using map_fn, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

parameter map_fn

The mapping function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The stream created from mapping each pair of elements from the given streams

val rev_map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

rev_map2 is equivalent to rev (map2 map_fn s1 s2)

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter map_fn

The mapping function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

returns

The stream created reversing the result of map2 map_fn s1 s2

val rev_map2_long : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'a -> 'b -> 'c t

rev_map2_long is equivalent to rev (map2_long map_fn s1 s2 s1_pl s2_pl)

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter map_fn

The mapping function to apply to each pair of elements

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The stream created reversing the result of map2_long map_fn s1 s2

val fold_left2 : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c

fold_left2 traverses both streams from left to right, combining each pair of adjacent elements with acc using fold_fn and returns the result of doing so, ending when one of the streams runs out of elements

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter map_fn

The mapping function to apply to each pair of elements

parameter acc

The initial value to use as the accumulator with fold_fn

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

returns

The stream created from mapping each pair of elements from the given streams

val fold_left2_lazy : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c t

fold_left2_lazy traverses the stream from left to right, combining each element with acc using fold_fn and returns each intermediary result in a stream from acc to the result of completely applying fold_left to the entire stream, ending when one of the streams runs out of elements

parameter map_fn

The mapping function to apply to each pair of elements

parameter acc

The initial value to use as the accumulator with fold_fn

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

returns

The stream created from mapping each pair of elements from the given streams

val fold_left2_long : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'a -> 'b -> 'c

fold_left2_long traverses both streams from left to right, combining each pair of adjacent elements with acc using fold_fn and returns the result of doing so, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter map_fn

The mapping function to apply to each pair of elements

parameter acc

The initial value to use as the accumulator with fold_fn

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The stream created from mapping each pair of elements from the given streams

val fold_left2_long_lazy : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'a -> 'b -> 'c t

fold_left2_long_lazy traverses the stream from left to right, combining each element with acc using fold_fn and returns each intermediary result in a stream from acc to the result of completely applying fold_left to the entire stream, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

parameter map_fn

The mapping function to apply to each pair of elements

parameter acc

The initial value to use as the accumulator with fold_fn

parameter s1

The stream whose elements each compose the first element in each pair that is used in the mapping

parameter s2

The stream whose elements each compose the second element in each pair that is used in the mapping

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

The stream created from mapping each pair of elements from the given streams

val for_all : ('a -> bool) -> 'a t -> bool

for_all returns true if applying pred to each elements of the stream results in true for all of them

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream whose elements are checked against the predicate

returns

true if all of the elements of the stream each return true when put into pred

val exists : ('a -> bool) -> 'a t -> bool

exists returns true if applying pred to any of the elements of the stream results in true

This involves an immediate traversal over all of the elements until an element meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which no element matches the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream whose elements are checked against the predicate

returns

true if any of the elements of the stream return true when put into pred

val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

for_all2 returns true if applying pred to each pair of adjacent element pairs from the given streams results in true for all of them

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter pred

The predicate that all of the elements are checked against

parameter s1

The stream whose elements are used as the first argument when checking against the predicate

parameter s2

The stream whose elements are used as the second argument when checking against the predicate

returns

true if all of the adjacent elements from the given streams each return true when put into pred

val for_all2_long : ('a -> 'b -> bool) -> 'a t -> 'b t -> 'a -> 'b -> bool

for_all2_long returns true if applying pred to each pair of adjacent element pairs from the given streams results in true for all of them, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter pred

The predicate that all of the elements are checked against

parameter s1

The stream whose elements are used as the first argument when checking against the predicate

parameter s2

The stream whose elements are used as the second argument when checking against the predicate

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

true if all of the adjacent elements from the given streams each return true when put into pred

val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

exists2 returns true if applying pred to any of the adjacent element pairs from the given streams results in true

This involves an immediate traversal over all of the elements until an element meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which no element matches the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s1

The stream whose elements are used as the first argument when checking against the predicate

parameter s2

The stream whose elements are used as the second argument when checking against the predicate

returns

true if any of the adjacent element pairs from the given streams return true when put into pred

val exists2_long : ('a -> 'b -> bool) -> 'a t -> 'b t -> 'a -> 'b -> bool

exists2_long returns true if applying pred to any of the adjacent element pairs from the given streams results in true, using placeholder values when one of the streams runs out of elements and ending when both streams run out of values

This involves an immediate traversal over all of the elements until an element meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which no element matches the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s1

The stream whose elements are used as the first argument when checking against the predicate

parameter s2

The stream whose elements are used as the second argument when checking against the predicate

parameter s1_pl

The value to use as a placeholder for s1 elements if s1 has less elements than s2

parameter s2_pl

The value to use as a placeholder for s2 elements if s2 has less elements than s1

returns

true if any of the adjacent element pairs from the given streams return true when put into pred

val mem : 'a -> 'a t -> bool

mem returns true if el is equal to an element in the given stream

This involves an immediate traversal over all of the elements until an element is found that is equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element is equivalent

parameter el

The element to search for in the given stream

parameter s

The stream in which to search for el

returns

true if el is equal to an element in the given stream

val memq : 'a -> 'a t -> bool

memq is the same as mem but uses physical equality instead of structural equality to compare elements

This involves an immediate traversal over all of the elements until an element is found that is equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element is equivalent

parameter el

The element to search for in the given stream

parameter s

The stream in which to search for el

returns

true if el is equal to an element in the given stream

val find_opt : ('a -> bool) -> 'a t -> 'a option

find_opt returns the first element of s that satisfies pred wrapped in Some or returns None if there is no such element

This involves an immediate traversal over all of the elements until an element meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which no element matches the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream that is searched through for an element that satisfies the predicate

returns

The first element of s that satisfies pred wrapped in Some or returns None if there is no such element

val find : ('a -> bool) -> 'a t -> 'a

find returns the first element of s that satisfies pred or raises Not_found if there is no such element

This involves an immediate traversal over all of the elements until an element meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which no element matches the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream that is searched through for an element that satisfies the predicate

returns

The first element of s that satisfies pred

raises Not_found

Raised if there is no element that satisfies pred

val partition : ('a -> bool) -> 'a t -> 'a t * 'a t

partition returns a pair of streams with the first stream being the stream of all the elements of s that satisfy pred and the second being the stream of all the elements of s that do not satisfy pred; the order of the elements in the input list is preserved

This involves an immediate traversal over all of the elements until an element meeting the predicate is found and an element not meeting the predicate is found, evaluating each of them, and not terminating on infinite streams in which either there are no elements that meet the predicate or all elements meet the predicate

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream that is partitioned into elements that do and do not satisfy the predicate

returns

A pair of streams with the first stream being the stream of all the elements of s that satisfy the pred and the second being the stream of all the elements of s that do not satisfy pred

val partition_lazy : ('a -> bool) -> 'a t -> 'a t lazy_t * 'a t lazy_t

partition_lazy returns a pair of computationally-delayed streams with the first stream being the stream of all the elements of s that satisfy pred and the second being the stream of all the elements of s that do not satisfy pred; the order of the elements in the input list is preserved

parameter pred

The predicate that all of the elements are checked against

parameter s

The stream that is partitioned into elements that do and do not satisfy the predicate

returns

A pair of computationally-delayed streams with the first stream being the stream of all the elements of s that satisfy pred and the second being the stream of all the elements of s that do not satisfy pred

val assoc_opt : 'a -> ('a * 'b) t -> 'b option

assoc_opt returns the value associated with el_key in the stream of pairs as_s wrapped in Some or None if there is no value associated with el_key in the as_s

This involves an immediate traversal over all of the elements until an element with an equivalent key is found, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The value associated with el_key in the stream of pairs as_s wrapped in Some or None if there is no value associated with el_key in the as_s

val assoc : 'a -> ('a * 'b) t -> 'b

assoc returns the value associated with el_key in the stream of pairs as_s or raises Not_found if there is no value associated with el_key in the as_s

This involves an immediate traversal over all of the elements until an element with an equivalent key is found, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The value associated with el_key in the stream of pairs as_s

raises Not_found

Raised if there is no value associated with el_key in the as_s

val assq_opt : 'a -> ('a * 'b) t -> 'b option

assq_opt is the same as assoc_opt but uses physical equality instead of structural equality to compare elements

This involves an immediate traversal over all of the elements until an element with an equivalent key is found, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The value associated with el_key in the stream of pairs as_s wrapped in Some or None if there is no value associated with el_key in the as_s

val assq : 'a -> ('a * 'b) t -> 'b

assq is the same as assoc but uses physical equality instead of structural equality to compare elements

This involves an immediate traversal over all of the elements until an element with an equivalent key is found, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The value associated with el_key in the stream of pairs as_s

raises Not_found

Raised if there is no value associated with el_key in the as_s

val mem_assoc : 'a -> ('a * 'b) t -> bool

mem_assoc returns true if there is a value associated with el_key in the stream of pairs as_s or false otherwise

This involves an immediate traversal over all of the elements until an element is found that has a key equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

true if there is a value associated with el_key in the stream of pairs as_s or false otherwise

val mem_assq : 'a -> ('a * 'b) t -> bool

mem_assq is the same as mem_assoc but uses physical equality instead of structural equality to compare elements

This involves an immediate traversal over all of the elements until an element is found that has a key equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter el_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

true if there is a value associated with el_key in the stream of pairs as_s or false otherwise

val remove_assoc : 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assoc returns the given stream without the first pair with a key equivalent to el_key

This involves an immediate traversal over all of the elements until an element is found that has a key equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter ell_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The given stream without the first pair with a key equivalent to el_key

val remove_assq : 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assq is the same as remove_assoc but uses physical equality instead of structural equality to compare elements

This involves an immediate traversal over all of the elements until an element is found that has a key equal to the given value, evaluating each of them, and not terminating on infinite streams in which no element has an equivalent key

parameter ell_key

The key that is searched for in the association stream

parameter as_s

The association stream that is searched through

returns

The given stream without the first pair with a key equivalent to el_key

val split : ('a * 'b) t -> 'a t * 'b t

split transforms a stream of pairs into a pair of streams

parameter s

The stream of pairs that will be broken apart into a pair of streams

returns

The pair of streams transformed from s

val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare returns 0 if s1 is equal to s2, a negative integer if s1 is less than s2, and a positive integer if s1 is greater than s2

This involves an immediate traversal over all of the elements of both streams until the end of one of the streams is reached or a pair of adjacent elements between the two streams are found to be not equivalent, meaning that this function does not terminate on two equivalent infinite streams

parameter compare_fn

The comparison function that returns 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller.

parameter s1

The stream on the left side of the comparison

parameter s2

The stream on the right side of the comparison

returns

0 if s1 is equal to s2, a negative integer if s1 is less than s2, and a positive integer if s1 is greater than s2

val equals : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool

compare returns true if s1 is equal to s2

This involves an immediate traversal over all of the elements of both streams until the end of one of the streams is reached or a pair of adjacent elements between the two streams are found to be not equivalent, meaning that this function does not terminate on two equivalent infinite streams

parameter equal_fn

The comparison function that returns true if its arguments compare as equal

parameter s1

The stream on the left side of the comparison

parameter s2

The stream on the right side of the comparison

returns

true if s1 is equal to s2

val sort : ('a -> 'a -> int) -> 'a t -> 'a t

sort sorts the given stream in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller. The resulting stream is sorted in increasing order. sort is guaranteed to run in constant heap space (in addition to the size of the result stream) and logarithmic stack space.

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter compare_fn

The comparison function described above

parameter s

The stream to be sorted

returns

The sorted stream in increasing order according to a compare_fn function

val stable_sort : ('a -> 'a -> int) -> 'a t -> 'a t

stable_sort is the same as sort, but the sorting algorithm is guaranteed to be stable

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter compare_fn

The comparison function as described in sort

parameter s

The stream to be sorted

returns

The sorted stream in increasing order according to a compare_fn function

val fast_sort : ('a -> 'a -> int) -> 'a t -> 'a t

fast_sort uses sort or stable_sort depending on which is faster on typical input

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter compare_fn

The comparison function as described in sort

parameter s

The stream to be sorted

returns

The sorted stream in increasing order according to a compare_fn function

val sort_uniq : ('a -> 'a -> int) -> 'a t -> 'a t

sort_uniq is the same as sort but also removes duplicates

This involves an immediate traversal over all of the elements, evaluating each of them before getting to the end, and not terminating on infinite streams

parameter compare_fn

The comparison function as described in sort

parameter s

The stream to be sorted

returns

The sorted stream in increasing order according to a compare_fn function

val merge : ('a -> 'a -> int) -> 'a t -> 'a t -> 'a t

merge merges two streams. Assuming that s1 and s2 are sorted according to the comparison function compare_fn, merge compare_fn s1 s2 will return a sorted stream containing all the elements of s1 and s2. If several elements compare equal, the elements of s1 will be before the elements of s2.

parameter compare_fn

The comparison function as described in sort

parameter s1

The first of the two streams that will be merged, takes precedence over s2 when equal

parameter s2

The second of the two streams that will be merged

returns

The result of merging the two given streams