<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" href="style.css" type="text/css"> <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"> <link rel="Start" href="index.html"> <link rel="previous" href="Batteries.DynArray.html"> <link rel="next" href="Batteries.File.html"> <link rel="Up" href="Batteries.html"> <link title="Index of types" rel=Appendix href="index_types.html"> <link title="Index of exceptions" rel=Appendix href="index_exceptions.html"> <link title="Index of values" rel=Appendix href="index_values.html"> <link title="Index of class methods" rel=Appendix href="index_methods.html"> <link title="Index of classes" rel=Appendix href="index_classes.html"> <link title="Index of modules" rel=Appendix href="index_modules.html"> <link title="Index of module types" rel=Appendix href="index_module_types.html"> <link title="BatArray" rel="Chapter" href="BatArray.html"> <link title="BatAvlTree" rel="Chapter" href="BatAvlTree.html"> <link title="BatBase64" rel="Chapter" href="BatBase64.html"> <link title="BatBig_int" rel="Chapter" href="BatBig_int.html"> <link title="BatBigarray" rel="Chapter" href="BatBigarray.html"> <link title="BatBitSet" rel="Chapter" href="BatBitSet.html"> <link title="BatBool" rel="Chapter" href="BatBool.html"> <link title="BatBounded" rel="Chapter" href="BatBounded.html"> <link title="BatBuffer" rel="Chapter" href="BatBuffer.html"> <link title="BatCache" rel="Chapter" href="BatCache.html"> <link title="BatChar" rel="Chapter" href="BatChar.html"> <link title="BatCharParser" rel="Chapter" href="BatCharParser.html"> <link title="BatComplex" rel="Chapter" href="BatComplex.html"> <link title="BatConcurrent" rel="Chapter" href="BatConcurrent.html"> <link title="BatDeque" rel="Chapter" href="BatDeque.html"> <link title="BatDigest" rel="Chapter" href="BatDigest.html"> <link title="BatDllist" rel="Chapter" href="BatDllist.html"> <link title="BatDynArray" rel="Chapter" href="BatDynArray.html"> <link title="BatEnum" rel="Chapter" href="BatEnum.html"> <link title="BatFile" rel="Chapter" href="BatFile.html"> <link title="BatFingerTree" rel="Chapter" href="BatFingerTree.html"> <link title="BatFloat" rel="Chapter" href="BatFloat.html"> <link title="BatFormat" rel="Chapter" href="BatFormat.html"> <link title="BatGc" rel="Chapter" href="BatGc.html"> <link title="BatGenlex" rel="Chapter" href="BatGenlex.html"> <link title="BatGlobal" rel="Chapter" href="BatGlobal.html"> <link title="BatHashcons" rel="Chapter" href="BatHashcons.html"> <link title="BatHashtbl" rel="Chapter" href="BatHashtbl.html"> <link title="BatHeap" rel="Chapter" href="BatHeap.html"> <link title="BatIMap" rel="Chapter" href="BatIMap.html"> <link title="BatIO" rel="Chapter" href="BatIO.html"> <link title="BatISet" rel="Chapter" href="BatISet.html"> <link title="BatInnerIO" rel="Chapter" href="BatInnerIO.html"> <link title="BatInnerPervasives" rel="Chapter" href="BatInnerPervasives.html"> <link title="BatInnerWeaktbl" rel="Chapter" href="BatInnerWeaktbl.html"> <link title="BatInt" rel="Chapter" href="BatInt.html"> <link title="BatInt32" rel="Chapter" href="BatInt32.html"> <link title="BatInt64" rel="Chapter" href="BatInt64.html"> <link title="BatInterfaces" rel="Chapter" href="BatInterfaces.html"> <link title="BatLazyList" rel="Chapter" href="BatLazyList.html"> <link title="BatLexing" rel="Chapter" href="BatLexing.html"> <link title="BatList" rel="Chapter" href="BatList.html"> <link title="BatLog" rel="Chapter" href="BatLog.html"> <link title="BatLogger" rel="Chapter" href="BatLogger.html"> <link title="BatMap" rel="Chapter" href="BatMap.html"> <link title="BatMarshal" rel="Chapter" href="BatMarshal.html"> <link title="BatMultiMap" rel="Chapter" href="BatMultiMap.html"> <link title="BatMultiPMap" rel="Chapter" href="BatMultiPMap.html"> <link title="BatMutex" rel="Chapter" href="BatMutex.html"> <link title="BatNativeint" rel="Chapter" href="BatNativeint.html"> <link title="BatNum" rel="Chapter" href="BatNum.html"> <link title="BatNumber" rel="Chapter" href="BatNumber.html"> <link title="BatOo" rel="Chapter" href="BatOo.html"> <link title="BatOptParse" rel="Chapter" href="BatOptParse.html"> <link title="BatOption" rel="Chapter" href="BatOption.html"> <link title="BatOrd" rel="Chapter" href="BatOrd.html"> <link title="BatParserCo" rel="Chapter" href="BatParserCo.html"> <link title="BatPathGen" rel="Chapter" href="BatPathGen.html"> <link title="BatPervasives" rel="Chapter" href="BatPervasives.html"> <link title="BatPrintexc" rel="Chapter" href="BatPrintexc.html"> <link title="BatPrintf" rel="Chapter" href="BatPrintf.html"> <link title="BatQueue" rel="Chapter" href="BatQueue.html"> <link title="BatRMutex" rel="Chapter" href="BatRMutex.html"> <link title="BatRandom" rel="Chapter" href="BatRandom.html"> <link title="BatRef" rel="Chapter" href="BatRef.html"> <link title="BatRefList" rel="Chapter" href="BatRefList.html"> <link title="BatResult" rel="Chapter" href="BatResult.html"> <link title="BatReturn" rel="Chapter" href="BatReturn.html"> <link title="BatScanf" rel="Chapter" href="BatScanf.html"> <link title="BatSeq" rel="Chapter" href="BatSeq.html"> <link title="BatSet" rel="Chapter" href="BatSet.html"> <link title="BatSplay" rel="Chapter" href="BatSplay.html"> <link title="BatStack" rel="Chapter" href="BatStack.html"> <link title="BatStream" rel="Chapter" href="BatStream.html"> <link title="BatString" rel="Chapter" href="BatString.html"> <link title="BatSubstring" rel="Chapter" href="BatSubstring.html"> <link title="BatSys" rel="Chapter" href="BatSys.html"> <link title="BatText" rel="Chapter" href="BatText.html"> <link title="BatTuple" rel="Chapter" href="BatTuple.html"> <link title="BatUChar" rel="Chapter" href="BatUChar.html"> <link title="BatUTF8" rel="Chapter" href="BatUTF8.html"> <link title="BatUnit" rel="Chapter" href="BatUnit.html"> <link title="BatUnix" rel="Chapter" href="BatUnix.html"> <link title="BatUref" rel="Chapter" href="BatUref.html"> <link title="BatVect" rel="Chapter" href="BatVect.html"> <link title="Batteries" rel="Chapter" href="Batteries.html"> <link title="BatteriesConfig" rel="Chapter" href="BatteriesConfig.html"> <link title="BatteriesPrint" rel="Chapter" href="BatteriesPrint.html"> <link title="BatteriesThread" rel="Chapter" href="BatteriesThread.html"> <link title="Extlib" rel="Chapter" href="Extlib.html"><link title="Final functions" rel="Section" href="#6_Finalfunctions"> <link title="Useful functions" rel="Section" href="#6_Usefulfunctions"> <link title="Lazy constructors" rel="Section" href="#6_Lazyconstructors"> <link title="Constructors" rel="Section" href="#6_Constructors"> <link title="Counting" rel="Section" href="#6_Counting"> <link title="Utilities " rel="Section" href="#6_Utilities"> <link title="Trampolining" rel="Section" href="#6_Trampolining"> <link title="Infix operators" rel="Section" href="#6_Infixoperators"> <link title="Monad related modules" rel="Section" href="#6_Monadrelatedmodules"> <link title="Boilerplate code" rel="Section" href="#6_Boilerplatecode"> <link title="Override modules" rel="Section" href="#6_Overridemodules"> <title>Batteries user guide : Batteries.Enum</title> </head> <body> <div class="navbar"><a class="pre" href="Batteries.DynArray.html" title="Batteries.DynArray">Previous</a> <a class="up" href="Batteries.html" title="Batteries">Up</a> <a class="post" href="Batteries.File.html" title="Batteries.File">Next</a> </div> <h1>Module <a href="type_Batteries.Enum.html">Batteries.Enum</a></h1> <pre><span class="keyword">module</span> Enum: <code class="type">BatEnum</code></pre><hr width="100%"> <pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">'a</code> t</span> </pre> <pre><span class="keyword">module type</span> <a href="BatEnum.Enumerable.html">Enumerable</a> = <code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.Enumerable.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info"> A signature for data structures which may be converted to and from <code class="code">enum</code>. </div> <pre><span class="keyword">include</span> <a href="BatEnum.Enumerable.html">BatEnum.Enumerable</a></pre> <pre><span class="keyword">include</span> <a href="BatInterfaces.Mappable.html">BatInterfaces.Mappable</a></pre> <br> <h6 id="6_Finalfunctions">Final functions</h6> <p> These functions consume the enumeration until it ends or an exception is raised by the first argument function.<br> <pre><span id="VALiter"><span class="keyword">val</span> iter</span> : <code class="type">('a -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> <code class="code">iter f e</code> calls the function <code class="code">f</code> with each elements of <code class="code">e</code> in turn.<br> </div> <pre><span id="VALiter2"><span class="keyword">val</span> iter2</span> : <code class="type">('a -> 'b -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> <code class="code">iter2 f e1 e2</code> calls the function <code class="code">f</code> with the next elements of <code class="code">e1</code> and <code class="code">e2</code> repeatedly until one of the two enumerations ends.<br> </div> <pre><span id="VALexists"><span class="keyword">val</span> exists</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> bool</code></pre><div class="info "> <code class="code">exists f e</code> returns <code class="code"><span class="keyword">true</span></code> if there is some <code class="code">x</code> in <code class="code">e</code> such that <code class="code">f x</code><br> </div> <pre><span id="VALfor_all"><span class="keyword">val</span> for_all</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> bool</code></pre><div class="info "> <code class="code">for_all f e</code> returns <code class="code"><span class="keyword">true</span></code> if for every <code class="code">x</code> in <code class="code">e</code>, <code class="code">f x</code> is true<br> </div> <pre><span id="VALfold"><span class="keyword">val</span> fold</span> : <code class="type">('b -> 'a -> 'b) -> 'b -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b</code></pre><div class="info "> A general loop on an enumeration. <p> If <code class="code">e</code> is empty, <code class="code">fold f v e</code> returns <code class="code">v</code>. Otherwise, <code class="code">fold v e</code> returns <code class="code">f (... (f (f v a0) a1) ...) aN</code> where <code class="code">a0,a1..aN</code> are the elements of <code class="code">e</code>. This function may be used, for instance, to compute the sum of all elements of an enumeration <code class="code">e</code> as follows: <code class="code">fold ( + ) 0 e</code>. Eager.<br> </div> <pre><span id="VALreduce"><span class="keyword">val</span> reduce</span> : <code class="type">('a -> 'a -> 'a) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a</code></pre><div class="info "> A simplified version of <code class="code">fold</code>, which uses the first element of the enumeration as a default value. <p> <code class="code">reduce f e</code> throws <code class="code"><span class="constructor">Not_found</span></code> if <code class="code">e</code> is empty, returns its only element if e is a singleton, otherwise <code class="code">f (... (f (f a0 a1) a2)...) aN</code> where <code class="code">a0,a1..aN</code> are the elements of <code class="code">e</code>.<br> </div> <pre><span id="VALsum"><span class="keyword">val</span> sum</span> : <code class="type">int <a href="BatEnum.html#TYPEt">t</a> -> int</code></pre><div class="info "> <code class="code">sum</code> returns the sum of the given int enum. If the argument is empty, returns 0. Eager<br> </div> <pre><span id="VALfsum"><span class="keyword">val</span> fsum</span> : <code class="type">float <a href="BatEnum.html#TYPEt">t</a> -> float</code></pre><div class="info "> <b>Since</b> 2.0<br> </div> <pre><span id="VALfold2"><span class="keyword">val</span> fold2</span> : <code class="type">('a -> 'b -> 'c -> 'c) -> 'c -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a> -> 'c</code></pre><div class="info "> <code class="code">fold2</code> is similar to <code class="code">fold</code> but will fold over two enumerations at the same time until one of the two enumerations ends.<br> </div> <pre><span id="VALscanl"><span class="keyword">val</span> scanl</span> : <code class="type">('b -> 'a -> 'b) -> 'b -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> A variant of <code class="code">fold</code> producing an enumeration of its intermediate values. If <code class="code">e</code> contains <code class="code">x0</code>, <code class="code">x1</code>, ..., <code class="code">scanl f init e</code> is the enumeration containing <code class="code">init</code>, <code class="code">f init x0</code>, <code class="code">f (f init x0) x1</code>... Lazy.<br> </div> <pre><span id="VALscan"><span class="keyword">val</span> scan</span> : <code class="type">('a -> 'a -> 'a) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">scan</code> is similar to <code class="code">scanl</code> but without the <code class="code">init</code> value: if <code class="code">e</code> contains <code class="code">x0</code>, <code class="code">x1</code>, <code class="code">x2</code> ..., <code class="code">scan f e</code> is the enumeration containing <code class="code">x0</code>, <code class="code">f x0 x1</code>, <code class="code">f (f x0 x1) x2</code>... <p> For instance, <code class="code">scan ( * ) (1 -- 10)</code> will produce an enumeration containing the successive values of the factorial function.<br> </div> <br> Indexed functions : these functions are similar to previous ones except that they call the function with one additional argument which is an index starting at 0 and incremented after each call to the function.<br> <pre><span id="VALiteri"><span class="keyword">val</span> iteri</span> : <code class="type">(int -> 'a -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre> <pre><span id="VALiter2i"><span class="keyword">val</span> iter2i</span> : <code class="type">(int -> 'a -> 'b -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre> <pre><span id="VALfoldi"><span class="keyword">val</span> foldi</span> : <code class="type">(int -> 'a -> 'b -> 'b) -> 'b -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b</code></pre> <pre><span id="VALfold2i"><span class="keyword">val</span> fold2i</span> : <code class="type">(int -> 'a -> 'b -> 'c -> 'c) -> 'c -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a> -> 'c</code></pre><br> <h6 id="6_Usefulfunctions">Useful functions</h6><br> <pre><span id="VALfind"><span class="keyword">val</span> find</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a</code></pre><div class="info "> <code class="code">find f e</code> returns the first element <code class="code">x</code> of <code class="code">e</code> such that <code class="code">f x</code> returns <code class="code"><span class="keyword">true</span></code>, consuming the enumeration up to and including the found element.<br> <b>Raises</b> <code>Not_found</code> if no such element exists in the enumeration, consuming the whole enumeration in the search. <p> Since <code class="code">find</code> (eagerly) consumes a prefix of the enumeration, it can be used several times on the same enumeration to find the next element.<br> </div> <pre><span id="VALfind_map"><span class="keyword">val</span> find_map</span> : <code class="type">('a -> 'b option) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b</code></pre><div class="info "> <code class="code">find_map f e</code> finds the first element <code class="code">x</code> of <code class="code">e</code> such that <code class="code">f x</code> returns <code class="code"><span class="constructor">Some</span> r</code>, then returns r. It consumes the enumeration up to and including the found element.<br> <b>Since</b> 2.0<br> <b>Raises</b> <code>Not_found</code> if no such element exists in the enumeration, consuming the whole enumeration in the search. <p> Since <code class="code">find_map</code> (eagerly) consumes a prefix of the enumeration, it can be used several times on the same enumeration to find the next element.<br> </div> <pre><span id="VALis_empty"><span class="keyword">val</span> is_empty</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> bool</code></pre><div class="info "> <code class="code">is_empty e</code> returns true if <code class="code">e</code> does not contains any element. Forces at most one element.<br> </div> <pre><span id="VALpeek"><span class="keyword">val</span> peek</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a option</code></pre><div class="info "> <code class="code">peek e</code> returns <code class="code"><span class="constructor">None</span></code> if <code class="code">e</code> is empty or <code class="code"><span class="constructor">Some</span> x</code> where <code class="code">x</code> is the next element of <code class="code">e</code>. The element is not removed from the enumeration.<br> </div> <pre><span id="VALget"><span class="keyword">val</span> get</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a option</code></pre><div class="info "> <code class="code">get e</code> returns <code class="code"><span class="constructor">None</span></code> if <code class="code">e</code> is empty or <code class="code"><span class="constructor">Some</span> x</code> where <code class="code">x</code> is the next element of <code class="code">e</code>, in which case the element is removed from the enumeration.<br> </div> <pre><span id="VALget_exn"><span class="keyword">val</span> get_exn</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a</code></pre><div class="info "> <code class="code">get_exn e</code> returns the first element of <code class="code">e</code>.<br> <b>Since</b> 2.0<br> <b>Raises</b> <code>No_more_elements</code> if <code class="code">e</code> is empty.<br> </div> <pre><span id="VALpush"><span class="keyword">val</span> push</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a -> unit</code></pre><div class="info "> <code class="code">push e x</code> will add <code class="code">x</code> at the beginning of <code class="code">e</code>.<br> </div> <pre><span id="VALjunk"><span class="keyword">val</span> junk</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> <code class="code">junk e</code> removes the first element from the enumeration, if any.<br> </div> <pre><span id="VALclone"><span class="keyword">val</span> clone</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">clone e</code> creates a new enumeration that is copy of <code class="code">e</code>. If <code class="code">e</code> is consumed by later operations, the clone will not get affected.<br> </div> <pre><span id="VALforce"><span class="keyword">val</span> force</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> <code class="code">force e</code> forces the application of all lazy functions and the enumeration of all elements, exhausting the enumeration. <p> An efficient intermediate data structure of enumerated elements is constructed and <code class="code">e</code> will now enumerate over that data structure.<br> </div> <pre><span id="VALtake"><span class="keyword">val</span> take</span> : <code class="type">int -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">take n e</code> returns the prefix of <code class="code">e</code> of length <code class="code">n</code>, or <code class="code">e</code> itself if <code class="code">n</code> is greater than the length of <code class="code">e</code><br> </div> <pre><span id="VALdrop"><span class="keyword">val</span> drop</span> : <code class="type">int -> 'a <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> <code class="code">drop n e</code> removes the first <code class="code">n</code> element from the enumeration, if any.<br> </div> <pre><span id="VALskip"><span class="keyword">val</span> skip</span> : <code class="type">int -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">skip n e</code> removes the first <code class="code">n</code> element from the enumeration, if any, then returns <code class="code">e</code>. <p> This function has the same behavior as <code class="code">drop</code> but is often easier to compose with, e.g., <code class="code">skip 5 %> take 3</code> is a new function which skips 5 elements and then returns the next 3 elements.<br> </div> <pre><span id="VALtake_while"><span class="keyword">val</span> take_while</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">take_while f e</code> produces a new enumeration in which only remain the first few elements <code class="code">x</code> of <code class="code">e</code> such that <code class="code">f x</code><br> </div> <pre><span id="VALdrop_while"><span class="keyword">val</span> drop_while</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">drop_while p e</code> produces a new enumeration in which only all the first elements such that <code class="code">f e</code> have been junked.<br> </div> <pre><span id="VALspan"><span class="keyword">val</span> span</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">span test e</code> produces two enumerations <code class="code">(hd, tl)</code>, such that <code class="code">hd</code> is the same as <code class="code">take_while test e</code> and <code class="code">tl</code> is the same as <code class="code">drop_while test e</code>.<br> </div> <pre><span id="VALbreak"><span class="keyword">val</span> break</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> Negated span. <code class="code">break test e</code> is equivalent to <code class="code">span (<span class="keyword">fun</span> x <span class="keywordsign">-></span> not (test x)) e</code><br> </div> <pre><span id="VALgroup"><span class="keyword">val</span> group</span> : <code class="type">('a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">group test e</code> divides <code class="code">e</code> into an enumeration of enumerations, where each sub-enumeration is the longest continuous enumeration of elements whose <code class="code">test</code> results are the same. <p> <code class="code"><span class="constructor">Enum</span>.group (x <span class="keywordsign">-></span> x <span class="keyword">mod</span> 2) [1;2;4;1] = [[1];[2;4];[1]]</code> <p> <code class="code"><span class="constructor">Enum</span>.group (<span class="keyword">fun</span> x <span class="keywordsign">-></span> x <span class="keyword">mod</span> 3) [1;2;4;1] = [[1];[2];[4;1]]</code> <p> <code class="code"><span class="constructor">Enum</span>.group (<span class="keyword">fun</span> s <span class="keywordsign">-></span> s.[0]) [<span class="string">"cat"</span>; <span class="string">"canary"</span>; <span class="string">"dog"</span>; <span class="string">"dodo"</span>; <span class="string">"ant"</span>; <span class="string">"cow"</span>] = [[<span class="string">"cat"</span>; <span class="string">"canary"</span>];[<span class="string">"dog"</span>;<span class="string">"dodo"</span>];[<span class="string">"ant"</span>];[<span class="string">"cow"</span>]]</code> <p> Warning: The result of this operation cannot be directly cloned safely; instead, reify to a non-lazy structure and read from that structure multiple times.<br> </div> <pre><span id="VALgroup_by"><span class="keyword">val</span> group_by</span> : <code class="type">('a -> 'a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">group_by eq e</code> divides <code class="code">e</code> into an enumeration of enumerations, where each sub-enumeration is the longest continuous enumeration of elements that are equal, as judged by <code class="code">eq</code>. <p> Warning: The result of this operation cannot be directly cloned safely; instead, reify to a non-lazy structure and read from that structure multiple times.<br> </div> <pre><span id="VALclump"><span class="keyword">val</span> clump</span> : <code class="type">int -> ('a -> unit) -> (unit -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">clump size add get e</code> runs <code class="code">add</code> on <code class="code">size</code> (or less at the end) elements of <code class="code">e</code> and then runs <code class="code">get</code> to produce value for the result enumeration. Useful to convert a char enum into string enum.<br> </div> <br> <h6 id="6_Lazyconstructors">Lazy constructors</h6> <p> These functions are lazy which means that they will create a new modified enumeration without actually enumerating any element until they are asked to do so by the programmer (using one of the functions above). <p> When the resulting enumerations of these functions are consumed, the underlying enumerations they were created from are also consumed.<br> <pre><span id="VALmap"><span class="keyword">val</span> map</span> : <code class="type">('a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">map f e</code> returns an enumeration over <code class="code">(f a0, f a1, ...)</code> where <code class="code">a0,a1...</code> are the elements of <code class="code">e</code>. Lazy.<br> </div> <pre><span id="VALmapi"><span class="keyword">val</span> mapi</span> : <code class="type">(int -> 'a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">mapi</code> is similar to <code class="code">map</code> except that <code class="code">f</code> is passed one extra argument which is the index of the element in the enumeration, starting from 0 : mapi f e returns an enumeration over <code class="code">(f 0 a0, f 1 a1, ...)</code> where <code class="code">a0,a1...</code> are the elements of <code class="code">e</code>.<br> </div> <pre><span id="VALfilter"><span class="keyword">val</span> filter</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">filter f e</code> returns an enumeration over all elements <code class="code">x</code> of <code class="code">e</code> such as <code class="code">f x</code> returns <code class="code"><span class="keyword">true</span></code>. Lazy. <p> <b>Note</b> filter is lazy in that it returns a lazy enumeration, but each element in the result is eagerly searched in the input enumeration. Therefore, the access to a given element in the result will diverge if it is preceded, in the input enumeration, by infinitely many false elements (elements on which the predicate <code class="code">p</code> returns <code class="code"><span class="keyword">false</span></code>). <p> Other functions that may drop an unbound number of elements (<code class="code">filter_map</code>, <code class="code">take_while</code>, etc.) have the same behavior.<br> </div> <pre><span id="VALfilter_map"><span class="keyword">val</span> filter_map</span> : <code class="type">('a -> 'b option) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">filter_map f e</code> returns an enumeration over all elements <code class="code">x</code> such as <code class="code">f y</code> returns <code class="code"><span class="constructor">Some</span> x</code> , where <code class="code">y</code> is an element of <code class="code">e</code>. <p> <code class="code">filter_map</code> works on infinite enumerations; see <code class="code">filter</code>.<br> </div> <pre><span id="VALappend"><span class="keyword">val</span> append</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">append e1 e2</code> returns an enumeration that will enumerate over all elements of <code class="code">e1</code> followed by all elements of <code class="code">e2</code>. Lazy. <p> <b>Note</b> The behavior of appending <code class="code">e</code> to itself or to something derived from <code class="code">e</code> is not specified. In particular, cloning <code class="code">append e e</code> may destroy any sharing between the first and the second argument.<br> </div> <pre><span id="VALprefix_action"><span class="keyword">val</span> prefix_action</span> : <code class="type">(unit -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">prefix_action f e</code> will behave as <code class="code">e</code> but guarantees that <code class="code">f ()</code> will be invoked exactly once before the current first element of <code class="code">e</code> is read. <p> If <code class="code">prefix_action f e</code> is cloned, <code class="code">f</code> is invoked only once, during the cloning. If <code class="code">prefix_action f e</code> is counted, <code class="code">f</code> is invoked only once, during the counting. <p> May be used for signalling that reading starts or for performing delayed evaluations.<br> </div> <pre><span id="VALsuffix_action"><span class="keyword">val</span> suffix_action</span> : <code class="type">(unit -> unit) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">suffix_action f e</code> will behave as <code class="code">e</code> but guarantees that <code class="code">f ()</code> will be invoked after the contents of <code class="code">e</code> are exhausted. <p> If <code class="code">suffix_action f e</code> is cloned, <code class="code">f</code> is invoked only once, when the original enumeration is exhausted. If <code class="code">suffix_action f e</code> is counted, <code class="code">f</code> is only invoked if the act of counting requires a call to <code class="code">force</code>. <p> May be used for signalling that reading stopped or for performing delayed evaluations.<br> </div> <pre><span id="VALconcat"><span class="keyword">val</span> concat</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">concat e</code> returns an enumeration over all elements of all enumerations of <code class="code">e</code>.<br> </div> <pre><span id="VALflatten"><span class="keyword">val</span> flatten</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> Synonym of <a href="BatEnum.html#VALconcat"><code class="code"><span class="constructor">BatEnum</span>.concat</code></a><br> </div> <br> <h6 id="6_Constructors">Constructors</h6> <p> In this section the word <i>shall</i> denotes a semantic requirement. The correct operation of the functions in this interface are conditional on the client meeting these requirements.<br> <pre><span id="EXCEPTIONNo_more_elements"><span class="keyword">exception</span> No_more_elements</span></pre> <div class="info "> This exception <i>shall</i> be raised by the <code class="code">next</code> function of <code class="code">make</code> or <code class="code">from</code> when no more elements can be enumerated, it <i>shall not</i> be raised by any function which is an argument to any other function specified in the interface.<br> </div> <pre><span id="EXCEPTIONInfinite_enum"><span class="keyword">exception</span> Infinite_enum</span></pre> <div class="info "> As a convenience for debugging, this exception <i>may</i> be raised by the <code class="code">count</code> function of <code class="code">make</code> when attempting to count an infinite enum.<br> </div> <pre><span id="VALempty"><span class="keyword">val</span> empty</span> : <code class="type">unit -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> The empty enumeration : contains no element<br> </div> <pre><span id="VALmake"><span class="keyword">val</span> make</span> : <code class="type">next:(unit -> 'a) -><br> count:(unit -> int) -> clone:(unit -> 'a <a href="BatEnum.html#TYPEt">t</a>) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> This function creates a fully defined enumeration. <p> <ul> <li>the <code class="code">next</code> function <i>shall</i> return the next element of the enumeration or raise <code class="code"><span class="constructor">No_more_elements</span></code> if the underlying data structure does not have any more elements to enumerate.</li> <li>the <code class="code">count</code> function <i>shall</i> return the actual number of remaining elements in the enumeration or <i>may</i> raise <code class="code"><span class="constructor">Infinite_enum</span></code> if it is known that the enumeration is infinite.</li> <li>the <code class="code">clone</code> function <i>shall</i> create a clone of the enumeration such as operations on the original enumeration will not affect the clone. </li> </ul> <p> For some samples on how to correctly use <code class="code">make</code>, you can have a look at implementation of <code class="code"><span class="constructor">BatList</span>.enum</code>.<br> </div> <pre><span id="VALfrom"><span class="keyword">val</span> from</span> : <code class="type">(unit -> 'a) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">from next</code> creates an enumeration from the <code class="code">next</code> function. <code class="code">next</code> <i>shall</i> return the next element of the enumeration or raise <code class="code"><span class="constructor">No_more_elements</span></code> when no more elements can be enumerated. Since the enumeration definition is incomplete, a call to <code class="code">count</code> will result in a call to <code class="code">force</code> that will enumerate all elements in order to return a correct value.<br> </div> <pre><span id="VALfrom_while"><span class="keyword">val</span> from_while</span> : <code class="type">(unit -> 'a option) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">from_while next</code> creates an enumeration from the <code class="code">next</code> function. <code class="code">next</code> <i>shall</i> return <code class="code"><span class="constructor">Some</span> x</code> where <code class="code">x</code> is the next element of the enumeration or <code class="code"><span class="constructor">None</span></code> when no more elements can be enumerated. Since the enumeration definition is incomplete, a call to <code class="code">clone</code> or <code class="code">count</code> will result in a call to <code class="code">force</code> that will enumerate all elements in order to return a correct value.<br> </div> <pre><span id="VALfrom_loop"><span class="keyword">val</span> from_loop</span> : <code class="type">'b -> ('b -> 'a * 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">from_loop data next</code> creates a (possibly infinite) enumeration from the successive results of applying <code class="code">next</code> to <code class="code">data</code>, then to the result, etc. The list ends whenever the function raises <a href="BatEnum.html#EXCEPTIONNo_more_elements"><code class="code"><span class="constructor">BatEnum</span>.<span class="constructor">No_more_elements</span></code></a>.<br> </div> <pre><span id="VALseq"><span class="keyword">val</span> seq</span> : <code class="type">'a -> ('a -> 'a) -> ('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">seq init step cond</code> creates a sequence of data, which starts from <code class="code">init</code>, extends by <code class="code">step</code>, until the condition <code class="code">cond</code> fails. E.g. <code class="code">seq 1 ((+) 1) ((>) 100)</code> returns <code class="code">1, 2, ... 99</code>. If <code class="code">cond init</code> is false, the result is empty.<br> </div> <pre><span id="VALunfold"><span class="keyword">val</span> unfold</span> : <code class="type">'b -> ('b -> ('a * 'b) option) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> As <code class="code">from_loop</code>, except uses option type to signal the end of the enumeration. <p> <code class="code">unfold data next</code> creates a (possibly infinite) enumeration from the successive results of applying <code class="code">next</code> to <code class="code">data</code>, then to the result, etc. The enumeration ends whenever the function returns <code class="code"><span class="constructor">None</span></code> <p> Example: <code class="code"><span class="constructor">Enum</span>.unfold n (<span class="keyword">fun</span> x <span class="keywordsign">-></span> <span class="keyword">if</span> x = 1 <span class="keyword">then</span> <span class="constructor">None</span> <span class="keyword">else</span> <span class="constructor">Some</span> (x, <span class="keyword">if</span> x <span class="keyword">land</span> 1 = 1 <span class="keyword">then</span> 3 * x + 1 <span class="keyword">else</span> x / 2))</code> returns the hailstone sequence starting at <code class="code">n</code>.<br> </div> <pre><span id="VALinit"><span class="keyword">val</span> init</span> : <code class="type">int -> (int -> 'a) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">init n f</code> creates a new enumeration over elements <code class="code">f 0, f 1, ..., f (n-1)</code><br> </div> <pre><span id="VALsingleton"><span class="keyword">val</span> singleton</span> : <code class="type">'a -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> Create an enumeration consisting of exactly one element.<br> </div> <pre><span id="VALrepeat"><span class="keyword">val</span> repeat</span> : <code class="type">?times:int -> 'a -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">repeat ~times:n x</code> creates a enum sequence filled with <code class="code">n</code> times of <code class="code">x</code>. It return infinite enum when <code class="code">~times</code> is absent. It returns empty enum when <code class="code">times <= 0</code><br> </div> <pre><span id="VALcycle"><span class="keyword">val</span> cycle</span> : <code class="type">?times:int -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">cycle</code> is similar to <code class="code">repeat</code>, except that the content to fill is a subenum rather than a single element. Note that <code class="code">times</code> represents the times of repeating not the length of enum. When <code class="code">~times</code> is absent the result is an infinite enum.<br> </div> <pre><span id="VALdelay"><span class="keyword">val</span> delay</span> : <code class="type">(unit -> 'a <a href="BatEnum.html#TYPEt">t</a>) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">delay (<span class="keyword">fun</span> () <span class="keywordsign">-></span> e)</code> produces an enumeration which behaves as <code class="code">e</code>. The enumeration itself will only be computed when consumed. <p> A typical use of this function is to explore lazily non-trivial data structures, as follows: <p> <code class="code"><span class="keyword">type</span> <span class="keywordsign">'</span>a tree = <span class="constructor">Leaf</span> <span class="keywordsign">|</span> <span class="constructor">Node</span> <span class="keyword">of</span> <span class="keywordsign">'</span>a * <span class="keywordsign">'</span>a tree * <span class="keywordsign">'</span>a tree <span class="keyword">let</span> enum_tree = <span class="keyword">let</span> <span class="keyword">rec</span> aux = <span class="keyword">function</span> <span class="keywordsign">|</span> <span class="constructor">Leaf</span> <span class="keywordsign">-></span> <span class="constructor">BatEnum</span>.empty () <span class="keywordsign">|</span> <span class="constructor">Node</span> (n, l, r) <span class="keywordsign">-></span> <span class="constructor">BatEnum</span>.append (<span class="constructor">BatEnum</span>.singleton n) (<span class="constructor">BatEnum</span>.append (delay (<span class="keyword">fun</span> () <span class="keywordsign">-></span> aux l)) (delay (<span class="keyword">fun</span> () <span class="keywordsign">-></span> aux r))) </code><br> </div> <pre><span id="VALto_object"><span class="keyword">val</span> to_object</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> (< clone : 'b; count : int; next : 'a > as 'b)</code></pre><div class="info "> <code class="code">to_object e</code> returns a representation of <code class="code">e</code> as an object.<br> </div> <pre><span id="VALof_object"><span class="keyword">val</span> of_object</span> : <code class="type">(< clone : 'b; count : int; next : 'a > as 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">of_object e</code> returns a representation of an object as an enumeration<br> </div> <pre><span id="VALenum"><span class="keyword">val</span> enum</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> identity : added for consistency with the other data structures<br> </div> <pre><span id="VALof_enum"><span class="keyword">val</span> of_enum</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> identity : added for consistency with the other data structures<br> </div> <br> <h6 id="6_Counting">Counting</h6><br> <pre><span id="VALcount"><span class="keyword">val</span> count</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> int</code></pre><div class="info "> <code class="code">count e</code> returns the number of remaining elements in <code class="code">e</code> without consuming the enumeration. <p> Depending of the underlying data structure that is implementing the enumeration functions, the count operation can be costly, and even sometimes can cause a call to <code class="code">force</code>.<br> </div> <pre><span id="VALfast_count"><span class="keyword">val</span> fast_count</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> bool</code></pre><div class="info "> For users worried about the speed of <code class="code">count</code> you can call the <code class="code">fast_count</code> function that will give an hint about <code class="code">count</code> implementation. Basically, if the enumeration has been created with <code class="code">make</code> or <code class="code">init</code> or if <code class="code">force</code> has been called on it, then <code class="code">fast_count</code> will return true.<br> </div> <pre><span id="VALhard_count"><span class="keyword">val</span> hard_count</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> int</code></pre><div class="info "> <code class="code">hard_count</code> returns the number of remaining in elements in <code class="code">e</code>, consuming the whole enumeration somewhere along the way. This function is always at least as fast as the fastest of either <code class="code">count</code> or a <code class="code">fold</code> on the elements of <code class="code">t</code>. <p> This function is useful when you have opened an enumeration for the sole purpose of counting its elements (e.g. the number of lines in a file).<br> </div> <br> <h6 id="6_Utilities">Utilities </h6><br> <pre><span id="VALrange"><span class="keyword">val</span> range</span> : <code class="type">?until:int -> int -> int <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">range p until:q</code> creates an enumeration of integers <code class="code">[p, p+1, ..., q]</code>. If <code class="code">until</code> is omitted, the enumeration is not bounded. Behaviour is not-specified once <code class="code">max_int</code> has been reached.<br> </div> <pre><span id="VALdup"><span class="keyword">val</span> dup</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">dup stream</code> returns a pair of streams which are identical to <code class="code">stream</code>. Note that stream is a destructive data structure, the point of <code class="code">dup</code> is to return two streams can be used independently.<br> </div> <pre><span id="VALcombine"><span class="keyword">val</span> combine</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> * 'b <a href="BatEnum.html#TYPEt">t</a> -> ('a * 'b) <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">combine</code> transform a pair of stream into a stream of pairs of corresponding elements. If one stream is short, excess elements of the longer stream are ignored.<br> </div> <pre><span id="VALuncombine"><span class="keyword">val</span> uncombine</span> : <code class="type">('a * 'b) <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">uncombine</code> is the opposite of <code class="code">combine</code><br> </div> <pre><span id="VALmerge"><span class="keyword">val</span> merge</span> : <code class="type">('a -> 'a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">merge test (a, b)</code> merge the elements from <code class="code">a</code> and <code class="code">b</code> into a single enumeration. At each step, <code class="code">test</code> is applied to the first element of <code class="code">a</code> and the first element of <code class="code">b</code> to determine which should get first into resulting enumeration. If <code class="code">a</code> or <code class="code">b</code> runs out of elements, the process will append all elements of the other enumeration to the result.<br> </div> <pre><span id="VALuniq"><span class="keyword">val</span> uniq</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">uniq e</code> returns a duplicate of <code class="code">e</code> with repeated values omitted. (similar to unix's <code class="code">uniq</code> command)<br> </div> <pre><span id="VALswitch"><span class="keyword">val</span> switch</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">switch test enum</code> splits <code class="code">enum</code> into two enums, where the first enum have all the elements satisfying <code class="code">test</code>, the second enum is opposite. The order of elements in the source enum is preserved.<br> </div> <pre><span id="VALpartition"><span class="keyword">val</span> partition</span> : <code class="type">('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> * 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> as <code class="code">switch</code><br> </div> <pre><span id="VALarg_min"><span class="keyword">val</span> arg_min</span> : <code class="type">('a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a</code></pre> <pre><span id="VALarg_max"><span class="keyword">val</span> arg_max</span> : <code class="type">('a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a</code></pre><div class="info "> <code class="code">arg_min f xs</code> returns the <code class="code">x</code> in <code class="code">xs</code> for which <code class="code">f x</code> is minimum. Similarly for <code class="code">arg_max</code>, except it returns the maximum. If multiple values reach the maximum, one of them is returned. (currently the first, but this is not guaranteed) <p> Example: <code class="code">-5 -- 5 |> arg_min (<span class="keyword">fun</span> x <span class="keywordsign">-></span> x * x + 6 * x - 5) = -3</code> Example: <code class="code"><span class="constructor">List</span>.enum [<span class="string">"cat"</span>; <span class="string">"canary"</span>; <span class="string">"dog"</span>; <span class="string">"dodo"</span>; <span class="string">"ant"</span>; <span class="string">"cow"</span>] |> arg_max <span class="constructor">String</span>.length = <span class="string">"canary"</span></code><br> <b>Raises</b> <code>Invalid_argument</code> if the input enum is empty<br> </div> <br> <h6 id="6_Trampolining">Trampolining</h6><br> <pre><span id="VALwhile_do"><span class="keyword">val</span> while_do</span> : <code class="type">('a -> bool) -><br> ('a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a>) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">while_do cont f e</code> is a loop on <code class="code">e</code> using <code class="code">f</code> as body and <code class="code">cont</code> as condition for continuing. <p> If <code class="code">e</code> contains elements <code class="code">x0</code>, <code class="code">x1</code>, <code class="code">x2</code>..., then if <code class="code">cont x0</code> is <code class="code"><span class="keyword">false</span></code>, <code class="code">x0</code> is returned as such and treatment stops. On the other hand, if <code class="code">cont x0</code> is <code class="code"><span class="keyword">true</span></code>, <code class="code">f x0</code> is returned and the loop proceeds with <code class="code">x1</code>... <p> Note that f is used as halting condition <i>after</i> the corresponding element has been added to the result stream.<br> </div> <br> <h6 id="6_Infixoperators">Infix operators</h6><br> <br> Infix versions of some functions <p> This module groups together all infix operators so that you can open it without opening the whole batEnum module.<br> <pre><span class="keyword">module</span> <a href="BatEnum.Infix.html">Infix</a>: <code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.Infix.html">..</a> <code class="code"><span class="keyword">end</span></code></pre> <pre><span id="VAL(--)"><span class="keyword">val</span> (--)</span> : <code class="type">int -> int -> int <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(--^)"><span class="keyword">val</span> (--^)</span> : <code class="type">int -> int -> int <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(--.)"><span class="keyword">val</span> (--.)</span> : <code class="type">float * float -> float -> float <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(---)"><span class="keyword">val</span> (---)</span> : <code class="type">int -> int -> int <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(--~)"><span class="keyword">val</span> (--~)</span> : <code class="type">char -> char -> char <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(//)"><span class="keyword">val</span> (//)</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> ('a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(/@)"><span class="keyword">val</span> (/@)</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> ('a -> 'b) -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(@/)"><span class="keyword">val</span> (@/)</span> : <code class="type">('a -> 'b) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(//@)"><span class="keyword">val</span> (//@)</span> : <code class="type">'a <a href="BatEnum.html#TYPEt">t</a> -> ('a -> 'b option) -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre> <pre><span id="VAL(@//)"><span class="keyword">val</span> (@//)</span> : <code class="type">('a -> 'b option) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'b <a href="BatEnum.html#TYPEt">t</a></code></pre><br> <h6 id="6_Monadrelatedmodules">Monad related modules</h6><br> <pre><span class="keyword">module</span> <a href="BatEnum.WithMonad.html">WithMonad</a>: <div class="sig_block"><code class="code"><span class="keyword">functor</span> (</code><code class="code"><span class="constructor">Mon</span></code><code class="code"> : </code><code class="type"><a href="BatInterfaces.Monad.html">BatInterfaces.Monad</a></code><code class="code">) <span class="keywordsign">-></span> </code><code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.WithMonad.html">..</a> <code class="code"><span class="keyword">end</span></code></div></pre><div class="info"> Monadic operations on Enumerations containing monadic elements </div> <pre><span class="keyword">module</span> <a href="BatEnum.Monad.html">Monad</a>: <code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.Monad.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info"> The BatEnum Monad </div> <br> <h6 id="6_Boilerplatecode">Boilerplate code</h6><br> <pre><span id="VALprint"><span class="keyword">val</span> print</span> : <code class="type">?first:string -><br> ?last:string -><br> ?sep:string -><br> ('a <a href="BatInnerIO.html#TYPEoutput">BatInnerIO.output</a> -> 'b -> unit) -><br> 'a <a href="BatInnerIO.html#TYPEoutput">BatInnerIO.output</a> -> 'b <a href="BatEnum.html#TYPEt">t</a> -> unit</code></pre><div class="info "> Print and consume the contents of an enumeration.<br> </div> <pre><span id="VALcompare"><span class="keyword">val</span> compare</span> : <code class="type">('a -> 'a -> int) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> -> int</code></pre><div class="info "> <code class="code">compare cmp a b</code> compares enumerations <code class="code">a</code> and <code class="code">b</code> by lexicographical order using comparison <code class="code">cmp</code>.<br> <b>Returns</b> <code class="code">compare cmp a' b'</code>, where <code class="code">a'</code> and <code class="code">b'</code> are respectively equal to <code class="code">a</code> and <code class="code">b</code> without their first element, if both <code class="code">a</code> and <code class="code">b</code> are non-empty and <code class="code">cmp x y = 0</code>, where <code class="code">x</code> is the first element of <code class="code">a</code> and <code class="code">y</code> is the first element of <code class="code">b</code><br> </div> <pre><span id="VALord"><span class="keyword">val</span> ord</span> : <code class="type">('a -> 'a -> <a href="BatOrd.html#TYPEorder">BatOrd.order</a>) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> -> <a href="BatOrd.html#TYPEorder">BatOrd.order</a></code></pre><div class="info "> Same as <code class="code">compare</code> but returning a <a href="BatOrd.html#TYPEorder"><code class="code"><span class="constructor">BatOrd</span>.order</code></a> instead of an interger.<br> </div> <pre><span id="VALequal"><span class="keyword">val</span> equal</span> : <code class="type">('a -> 'a -> bool) -> 'a <a href="BatEnum.html#TYPEt">t</a> -> 'a <a href="BatEnum.html#TYPEt">t</a> -> bool</code></pre><div class="info "> <code class="code">equal eq a b</code> returns <code class="code"><span class="keyword">true</span></code> when <code class="code">a</code> and <code class="code">b</code> contain the same sequence of elements.<br> </div> <br> <h6 id="6_Overridemodules">Override modules</h6><br> <br> The following modules replace functions defined in <a href="BatEnum.html"><code class="code"><span class="constructor">BatEnum</span></code></a> with functions behaving slightly differently but having the same name. This is by design: the functions meant to override the corresponding functions of <a href="BatEnum.html"><code class="code"><span class="constructor">BatEnum</span></code></a>.<br> <pre><span class="keyword">module</span> <a href="BatEnum.Exceptionless.html">Exceptionless</a>: <code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.Exceptionless.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info"> Operations on <a href="BatEnum.html"><code class="code"><span class="constructor">BatEnum</span></code></a> without exceptions. </div> <pre><span class="keyword">module</span> <a href="BatEnum.Labels.html">Labels</a>: <code class="code"><span class="keyword">sig</span></code> <a href="BatEnum.Labels.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info"> Operations on <a href="BatEnum.html"><code class="code"><span class="constructor">BatEnum</span></code></a> with labels. </div> </body></html>