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_tThe type associated with
LazyStreamNilindicates the end of the stream as[]does forlistsConsis similar to::forlists, thoughConshas an eagerly-evaluated head and a lazily-evaluated tail
val empty : 'a tThe empty stream, which is equivalent to
Nil
val is_empty : 'a t -> boolis_emptyreturnstrueif the given stream is equivalent toNil- parameter s
The stream to compare to
Nil
- returns
trueif the stream is equivalent toNil
val cons : 'a -> 'a t -> 'a tconsconcatenateselto the beginning ofs- 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 tfromreturns an infinite stream with the first element beingstartand all successive elements being the result of applyingnextto 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
startand each successive element being the result of applyingnextto the previous element
val from_int : int -> int -> int tfrom_intreturns an infinite stream ofints with the first element beingstartand each successive element being the previous element plusinc- 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 beingstartand each successive element being the previous element plusinc
val from_char : char -> int -> char tfrom_charreturns an infinite stream ofchars with the first element beingstartand each successive element being the character with the character code of the sum of the previous character code andinc- parameter start
The starting
charof 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 beingstartand each successive element the character with the character code of the sum of the previous character code andinc
val range : 'a -> 'a -> ('a -> 'a) -> 'a trangereturns a stream with the first element beingstart, all successive elements being the result of applyingnextto the previous element, and the final element beingfinal- 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 applyingnextto the previous element, and the final element beingfinish
val range_int : int -> int -> int -> int trange_intreturns a stream ofints with the first element beingstart, each successive element being the previous element plusinc, and the final element beingfinish- parameter start
The starting
intof the stream
- parameter final
The ending
intof 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 beingstart, each successive element being the previous element plusinc, and the final element beingfinish
val range_char : char -> char -> int -> char trange_charreturns a stream ofchars with the first element beingstart, each successive element being the character with the character code of the sum of the previous character code andinc, and the final element beingfinish- parameter start
The starting
charof the stream
- parameter final
The ending
charof 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 beingstart, each successive element being character with the character code of the sum of the previous character code andinc, and the final element beingfinish
val filter : ('a -> bool) -> 'a t -> 'a tfilterreturns a stream that contains all of the elements from the given stream that returntruewhen inputted intofilter_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
truewhen inputted intofilter_fn
val append : 'a t -> 'a t -> 'a tappendconcatenates 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 tappend_lazyconcatenates 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 tmapiis the same asmap, 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 tmapapplies the functionmap_fnto each element of the given stream and builds a stream with the result of mapping each element usingmap_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 tfilter_mapappliesfm_fnto every element ofs, filters out theNoneelements, and returns the stream of the arguments of theSomeelements- 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_mapto every element ofsand filtering out theNoneelements
val flat_map : ('a -> 'b t) -> 'a t -> 'b tflat_mapmaps each element to a substream usingfm_fnand then returns the stream resulting from concatenating the substreams, usingappendto 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_fnand then concatenating each of the substreams
val flat_map_lazy : ('a -> 'b t) -> 'a t -> 'b tflat_map_lazymaps each element to a substream usingfm_fnand then returns the stream resulting from concatenating the substreams, usingappend_lazyto 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_fnand then concatenating each of the substreams
val map_mult : ('c -> 'a -> 'b) list -> ('a -> 'c) -> 'a t -> 'b tmap_multmaps each element to a number of outputs, mapping each element using each function inmap_fnsand producing as many elements of output for every element of input as is the length ofmap_fnsAdditionally, for each element taken as input, a value is computed using
shared_fnthat is passed into all of the mapping functions and can be used to prevent redundant computation among the mapping functionsExamples (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
sand the element ofsto 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_fnsto each element ins, using the value produced fromshared_fnas one of the inputs to each function inmap_fns
val zip : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tzipreturns 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
s1ands2to 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 tzip_longreturns 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
s1ands2to 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- 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 -> 'ahdreturns the first element of the given stream, raisingFailure "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 ttlreturns the given stream without its first element, raisingFailure "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 listtakereturns the firstnelements of the given stream as alist, but if there are less thennelements 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
nelements of the given stream as alist, but if there are less thennelements in the stream, it returns the remaining elements from the stream
val take_except : int -> 'a t -> 'a listtake_exceptreturns the firstnelements of the given stream as alist, but if there are less thennelements in the stream, it raisesFailure "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
nelements of the given stream as alist
- raises Failure
Raised if the given stream has less than
nelements
val take_all : 'a t -> 'a listtake_allreturns the given stream as alistThis 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 tdropreturns the given stream without its firstnelements, but if there are less thannelements in the stream, then it simply returnsNil- 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
nelements, but if there are less thannelements in the stream, then it simply returnsNil
val drop_except : int -> 'a t -> 'a tdrop_exceptreturns the given stream without its firstnelements, but if there are less thannelements in the stream, then it raisesFailure "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
nelements
- raises Failure
Raised if there are less than
nelements
val to_seq : 'a t -> 'a Stdlib.Seq.tto_seqconverts the given stream to a Seq.t- parameter s
The stream to convert to a sequence
- returns
sas a sequence
val of_seq : 'a Stdlib.Seq.t -> 'a tof_seqconverts the given Seq.t to a stream- parameter seq
The sequence to convert to a stream
- returns
seqas a stream
val of_list : 'a list -> 'a tof_listconverts the givenlistto a stream- parameter lst
The list to convert to a stream
- returns
lstas a stream
val to_string : char t -> stringto_stringconverts the givencharstream to astringThis 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
charstream to convert to astring
- returns
sas astring
val of_string : string -> char tof_stringconverts the givenstringto acharstream- parameter str
The
stringto convert to acharstream
- returns
stras acharstream
val to_bytes : char t -> bytesto_bytesconverts the givencharstream tobytesThis 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
charstream to convert tobytes
- returns
sasbytes
val of_bytes : bytes -> char tof_stringconverts the givenbytesto acharstream- parameter b
The
bytesto convert to acharstream
- returns
bas acharstream
val output_to_channel : Stdlib.out_channel -> char t -> unitoutput_to_channeloutputs each of thechars in the givencharstream into the givenout_channelThis 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 tof_channellazily readschars from the givenin_channeland returns acharstream- 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 trepeat_elementsreturns a stream with all of the given stream's elements each repeatedntimes 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
ntimes in a row
val repeat_stream : int -> 'a t -> 'a trepeat_streamreturns a stream that essentially is the given stream repeatedntimes 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
ntimes in a row
val cartesian_product : 'a t -> 'b t -> ('a * 'b) tcartesian_productReturns 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 trevreverses the given streamThis 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 -> 'bfold_lefttraverses the stream from left to right, combining each element withaccusingfold_fnand returns the result of doing soThis 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_fnwith 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 tfold_left_lazytraverses the stream from left to right, combining each element withaccusingfold_fnand returns each intermediary result in a stream fromaccto the result of completely applyingfold_leftto 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_fnwith the accumulator and the current element through the entire stream from left to right
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'bfold_righttraverses the stream from right to left, combining each element withaccusingfold_fnand returns the result of doing soThis 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_fnwith the accumulator and the current element through the entire stream from right to left
val length : 'a t -> intlengthreturns the number of elements in the given streamThis 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 -> intcompare_lengthscompares the lengths of the two given streams, andcompare_lengths s1 s2is equivalent tocompare (length s1) (length s2), except that the computation stops after iteration completes on the shortest streamThis 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 optionnth_optreturns thenth element of the given stream wrapped inSome, but if the stream has less thannelements, thenNoneis 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 inSome, but if the stream has less thannelements, thenNoneis returned
val nth : 'a t -> int -> 'anthreturns thenth element of the given stream, but if the stream has less thannelements, thenInvalid_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 inSome
- raises Invalid_arg
Raised if the stream has less than
nelements
val init : int -> (int -> 'a) -> 'a tinitcreates a stream whose elements are composed using calls toinit_fnwith the integers0throughlen - 1inclusive, raisingInvalid_arg "LazyStream.init"iflenis negative- parameter len
The final length of the stream
- parameter init_fn
The function used to map the integers
0throughlen - 1to values of this stream
- returns
The stream whose elements are composed using calls to
init_fnwith the integers0throughlen - 1inclusive
- raises Invalid_arg
Raised if
lenis negative
val rev_append : 'a t -> 'a t -> 'a trev_appendreversess1and concatenates it tos2, which is equivalent toappend (rev s1) s2This 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
s1will be concatenate
- returns
The stream created by reversing
s1and concatenating it tos2
val rev_append_lazy : 'a t -> 'a t lazy_t -> 'a trev_appendreversess1and concatenates it to the computationally-delayeds2, making this function equivalent toappend_lazy (rev s1) s2This 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
s1will be concatenate
- returns
The stream created by reversing
s1and concatenating it tos2
val concat : 'a t t -> 'a tconcatconcatenates a stream of streams, meaning that the elements ofssare all concatenated together in the same order, usingappend- 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 tconcat_lazyconcatenates a stream of streams, meaning that the elements ofssare all concatenated together in the same order, usingappend_lazy- parameter ss
The stream of streams to concatenate
- returns
The concatenation of the streams that compose the given stream
val flatten_lazy : 'a t t -> 'a tflatten_lazyis an alias ofconcat_lazy
val iteri : (int -> 'a -> unit) -> 'a t -> unititeriis the same asiter, butiter_fnis applied to the index of the element as the first argument and the element itself as the second argumentThis 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 -> unititerappliesiter_fnto each element of the given streamThis 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) tcombinetransforms the pair of streamss1ands2into 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
s1ands2, ending when one of the streams runs out of elements
val combine_long : 'a t -> 'b t -> 'a -> 'b -> ('a * 'b) tcombine_longtransforms the pair of streamss1ands2into 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- returns
The stream of pairs composed from the pair of streams
s1ands2, 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 -> unititer2applies the functioniter_fnto each pair of elements from the given streams, ending when one of the streams runs out of elementsThis 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 -> unititer2_longapplies the functioniter_fnto 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 valuesThis 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- returns
Unit
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tmap2applies the functionmap_fnto each pair of elements from the given streams and builds a stream with the result of mapping each pair of elements usingmap_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 tmap2_longapplies the functionmap_fnto each pair of elements from the given streams and builds a stream with the result of mapping each pair of elements usingmap_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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- 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 trev_map2is equivalent torev (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 trev_map2_longis equivalent torev (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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- 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 -> 'cfold_left2traverses both streams from left to right, combining each pair of adjacent elements withaccusingfold_fnand returns the result of doing so, ending when one of the streams runs out of elementsThis 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 tfold_left2_lazytraverses the stream from left to right, combining each element withaccusingfold_fnand returns each intermediary result in a stream fromaccto the result of completely applyingfold_leftto 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 -> 'cfold_left2_longtraverses both streams from left to right, combining each pair of adjacent elements withaccusingfold_fnand 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 valuesThis 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- 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 tfold_left2_long_lazytraverses the stream from left to right, combining each element withaccusingfold_fnand returns each intermediary result in a stream fromaccto the result of completely applyingfold_leftto 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- returns
The stream created from mapping each pair of elements from the given streams
val for_all : ('a -> bool) -> 'a t -> boolfor_allreturnstrueif applyingpredto each elements of the stream results intruefor all of themThis 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
trueif all of the elements of the stream each returntruewhen put intopred
val exists : ('a -> bool) -> 'a t -> boolexistsreturnstrueif applyingpredto any of the elements of the stream results intrueThis 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
trueif any of the elements of the stream returntruewhen put intopred
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> boolfor_all2returnstrueif applyingpredto each pair of adjacent element pairs from the given streams results intruefor all of themThis 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
trueif all of the adjacent elements from the given streams each returntruewhen put intopred
val for_all2_long : ('a -> 'b -> bool) -> 'a t -> 'b t -> 'a -> 'b -> boolfor_all2_longreturnstrueif applyingpredto each pair of adjacent element pairs from the given streams results intruefor all of them, using placeholder values when one of the streams runs out of elements and ending when both streams run out of valuesThis 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- returns
trueif all of the adjacent elements from the given streams each returntruewhen put intopred
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> boolexists2returnstrueif applyingpredto any of the adjacent element pairs from the given streams results intrueThis 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
trueif any of the adjacent element pairs from the given streams returntruewhen put intopred
val exists2_long : ('a -> 'b -> bool) -> 'a t -> 'b t -> 'a -> 'b -> boolexists2_longreturnstrueif applyingpredto any of the adjacent element pairs from the given streams results intrue, using placeholder values when one of the streams runs out of elements and ending when both streams run out of valuesThis 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
s1elements ifs1has less elements thans2
- parameter s2_pl
The value to use as a placeholder for
s2elements ifs2has less elements thans1
- returns
trueif any of the adjacent element pairs from the given streams returntruewhen put intopred
val mem : 'a -> 'a t -> boolmemreturnstrueifelis equal to an element in the given streamThis 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
trueifelis equal to an element in the given stream
val memq : 'a -> 'a t -> boolmemqis the same asmembut uses physical equality instead of structural equality to compare elementsThis 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
trueifelis equal to an element in the given stream
val find_opt : ('a -> bool) -> 'a t -> 'a optionfind_optreturns the first element ofsthat satisfiespredwrapped inSomeor returnsNoneif there is no such elementThis 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
sthat satisfiespredwrapped inSomeor returnsNoneif there is no such element
val find : ('a -> bool) -> 'a t -> 'afindreturns the first element ofsthat satisfiespredor raisesNot_foundif there is no such elementThis 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
sthat satisfiespred
- raises Not_found
Raised if there is no element that satisfies
pred
val partition : ('a -> bool) -> 'a t -> 'a t * 'a tpartitionreturns a pair of streams with the first stream being the stream of all the elements ofsthat satisfypredand the second being the stream of all the elements ofsthat do not satisfypred; the order of the elements in the input list is preservedThis 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
sthat satisfy thepredand the second being the stream of all the elements ofsthat do not satisfypred
val partition_lazy : ('a -> bool) -> 'a t -> 'a t lazy_t * 'a t lazy_tpartition_lazyreturns a pair of computationally-delayed streams with the first stream being the stream of all the elements ofsthat satisfypredand the second being the stream of all the elements ofsthat do not satisfypred; 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
sthat satisfypredand the second being the stream of all the elements ofsthat do not satisfypred
val assoc_opt : 'a -> ('a * 'b) t -> 'b optionassoc_optreturns the value associated withel_keyin the stream of pairsas_swrapped inSomeorNoneif there is no value associated withel_keyin theas_sThis 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_keyin the stream of pairsas_swrapped inSomeorNoneif there is no value associated withel_keyin theas_s
val assoc : 'a -> ('a * 'b) t -> 'bassocreturns the value associated withel_keyin the stream of pairsas_sor raisesNot_foundif there is no value associated withel_keyin theas_sThis 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_keyin the stream of pairsas_s
- raises Not_found
Raised if there is no value associated with
el_keyin theas_s
val assq_opt : 'a -> ('a * 'b) t -> 'b optionassq_optis the same asassoc_optbut uses physical equality instead of structural equality to compare elementsThis 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_keyin the stream of pairsas_swrapped inSomeorNoneif there is no value associated withel_keyin theas_s
val assq : 'a -> ('a * 'b) t -> 'bassqis the same asassocbut uses physical equality instead of structural equality to compare elementsThis 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_keyin the stream of pairsas_s
- raises Not_found
Raised if there is no value associated with
el_keyin theas_s
val mem_assoc : 'a -> ('a * 'b) t -> boolmem_assocreturnstrueif there is a value associated withel_keyin the stream of pairsas_sorfalseotherwiseThis 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
trueif there is a value associated withel_keyin the stream of pairsas_sorfalseotherwise
val mem_assq : 'a -> ('a * 'b) t -> boolmem_assqis the same asmem_assocbut uses physical equality instead of structural equality to compare elementsThis 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
trueif there is a value associated withel_keyin the stream of pairsas_sorfalseotherwise
val remove_assoc : 'a -> ('a * 'b) t -> ('a * 'b) tremove_assocreturns the given stream without the first pair with a key equivalent toel_keyThis 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) tremove_assqis the same asremove_assocbut uses physical equality instead of structural equality to compare elementsThis 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 tsplittransforms 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 -> intcomparereturns 0 ifs1is equal tos2, a negative integer ifs1is less thans2, and a positive integer ifs1is greater thans2This 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
s1is equal tos2, a negative integer ifs1is less thans2, and a positive integer ifs1is greater thans2
val equals : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolcomparereturnstrueifs1is equal tos2This 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
trueifs1is equal tos2
val sort : ('a -> 'a -> int) -> 'a t -> 'a tsortsorts 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.sortis 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_fnfunction
val stable_sort : ('a -> 'a -> int) -> 'a t -> 'a tstable_sortis the same assort, but the sorting algorithm is guaranteed to be stableThis 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_fnfunction
val fast_sort : ('a -> 'a -> int) -> 'a t -> 'a tfast_sortusessortorstable_sortdepending on which is faster on typical inputThis 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_fnfunction
val sort_uniq : ('a -> 'a -> int) -> 'a t -> 'a tsort_uniqis the same assortbut also removes duplicatesThis 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_fnfunction
val merge : ('a -> 'a -> int) -> 'a t -> 'a t -> 'a tmergemerges two streams. Assuming thats1ands2are sorted according to the comparison functioncompare_fn,merge compare_fn s1 s2will return a sorted stream containing all the elements ofs1ands2. If several elements compare equal, the elements ofs1will be before the elements ofs2.- 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
s2when equal
- parameter s2
The second of the two streams that will be merged
- returns
The result of merging the two given streams