ocaml-containers/0.17/CCPrint.html
2016-04-22 23:45:40 +02:00

208 lines
No EOL
15 KiB
HTML

<!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=utf-8" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="previous" href="CCVector.html">
<link rel="next" href="CCError.html">
<link rel="Up" href="index.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 class types" rel=Appendix href="index_class_types.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="CCVector" rel="Chapter" href="CCVector.html">
<link title="CCPrint" rel="Chapter" href="CCPrint.html">
<link title="CCError" rel="Chapter" href="CCError.html">
<link title="CCHeap" rel="Chapter" href="CCHeap.html">
<link title="CCList" rel="Chapter" href="CCList.html">
<link title="CCOpt" rel="Chapter" href="CCOpt.html">
<link title="CCPair" rel="Chapter" href="CCPair.html">
<link title="CCFun" rel="Chapter" href="CCFun.html">
<link title="CCHash" rel="Chapter" href="CCHash.html">
<link title="CCInt" rel="Chapter" href="CCInt.html">
<link title="CCBool" rel="Chapter" href="CCBool.html">
<link title="CCFloat" rel="Chapter" href="CCFloat.html">
<link title="CCArray" rel="Chapter" href="CCArray.html">
<link title="CCRef" rel="Chapter" href="CCRef.html">
<link title="CCSet" rel="Chapter" href="CCSet.html">
<link title="CCOrd" rel="Chapter" href="CCOrd.html">
<link title="CCRandom" rel="Chapter" href="CCRandom.html">
<link title="CCString" rel="Chapter" href="CCString.html">
<link title="CCHashtbl" rel="Chapter" href="CCHashtbl.html">
<link title="CCMap" rel="Chapter" href="CCMap.html">
<link title="CCFormat" rel="Chapter" href="CCFormat.html">
<link title="CCIO" rel="Chapter" href="CCIO.html">
<link title="CCInt64" rel="Chapter" href="CCInt64.html">
<link title="CCChar" rel="Chapter" href="CCChar.html">
<link title="CCResult" rel="Chapter" href="CCResult.html">
<link title="Containers" rel="Chapter" href="Containers.html">
<link title="CCKTree" rel="Chapter" href="CCKTree.html">
<link title="CCKList" rel="Chapter" href="CCKList.html">
<link title="CCLazy_list" rel="Chapter" href="CCLazy_list.html">
<link title="CCMultiMap" rel="Chapter" href="CCMultiMap.html">
<link title="CCMultiSet" rel="Chapter" href="CCMultiSet.html">
<link title="CCTrie" rel="Chapter" href="CCTrie.html">
<link title="CCFlatHashtbl" rel="Chapter" href="CCFlatHashtbl.html">
<link title="CCCache" rel="Chapter" href="CCCache.html">
<link title="CCPersistentHashtbl" rel="Chapter" href="CCPersistentHashtbl.html">
<link title="CCDeque" rel="Chapter" href="CCDeque.html">
<link title="CCFQueue" rel="Chapter" href="CCFQueue.html">
<link title="CCBV" rel="Chapter" href="CCBV.html">
<link title="CCMixtbl" rel="Chapter" href="CCMixtbl.html">
<link title="CCMixmap" rel="Chapter" href="CCMixmap.html">
<link title="CCRingBuffer" rel="Chapter" href="CCRingBuffer.html">
<link title="CCIntMap" rel="Chapter" href="CCIntMap.html">
<link title="CCPersistentArray" rel="Chapter" href="CCPersistentArray.html">
<link title="CCMixset" rel="Chapter" href="CCMixset.html">
<link title="CCHashconsedSet" rel="Chapter" href="CCHashconsedSet.html">
<link title="CCGraph" rel="Chapter" href="CCGraph.html">
<link title="CCHashSet" rel="Chapter" href="CCHashSet.html">
<link title="CCBitField" rel="Chapter" href="CCBitField.html">
<link title="CCHashTrie" rel="Chapter" href="CCHashTrie.html">
<link title="CCBloom" rel="Chapter" href="CCBloom.html">
<link title="CCWBTree" rel="Chapter" href="CCWBTree.html">
<link title="CCRAL" rel="Chapter" href="CCRAL.html">
<link title="CCAllocCache" rel="Chapter" href="CCAllocCache.html">
<link title="CCImmutArray" rel="Chapter" href="CCImmutArray.html">
<link title="CCHet" rel="Chapter" href="CCHet.html">
<link title="Containers_string" rel="Chapter" href="Containers_string.html">
<link title="CCKMP" rel="Chapter" href="CCKMP.html">
<link title="CCLevenshtein" rel="Chapter" href="CCLevenshtein.html">
<link title="CCApp_parse" rel="Chapter" href="CCApp_parse.html">
<link title="CCParse" rel="Chapter" href="CCParse.html">
<link title="CCBigstring" rel="Chapter" href="CCBigstring.html">
<link title="CCArray1" rel="Chapter" href="CCArray1.html">
<link title="CCPool" rel="Chapter" href="CCPool.html">
<link title="CCLock" rel="Chapter" href="CCLock.html">
<link title="CCSemaphore" rel="Chapter" href="CCSemaphore.html">
<link title="CCThread" rel="Chapter" href="CCThread.html">
<link title="CCBlockingQueue" rel="Chapter" href="CCBlockingQueue.html">
<link title="CCTimer" rel="Chapter" href="CCTimer.html">
<link title="Containers_advanced" rel="Chapter" href="Containers_advanced.html">
<link title="CCLinq" rel="Chapter" href="CCLinq.html">
<link title="CCBatch" rel="Chapter" href="CCBatch.html">
<link title="CCCat" rel="Chapter" href="CCCat.html">
<link title="CCMonadIO" rel="Chapter" href="CCMonadIO.html">
<link title="Containers_io_is_deprecated" rel="Chapter" href="Containers_io_is_deprecated.html">
<link title="CCUnix" rel="Chapter" href="CCUnix.html">
<link title="CCSexp" rel="Chapter" href="CCSexp.html">
<link title="CCSexpM" rel="Chapter" href="CCSexpM.html"><link title="Combinators" rel="Section" href="#2_Combinators">
<link title="IO" rel="Section" href="#2_IO">
<link title="Monadic IO" rel="Section" href="#2_MonadicIO">
<title>Containers doc : CCPrint</title>
</head>
<body>
<div class="navbar"><a class="pre" href="CCVector.html" title="CCVector">Previous</a>
&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;<a class="post" href="CCError.html" title="CCError">Next</a>
</div>
<h1>Module <a href="type_CCPrint.html">CCPrint</a></h1>
<pre><span class="keyword">module</span> CCPrint: <code class="code"><span class="keyword">sig</span></code> <a href="CCPrint.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info module top">
<h1 id="1_PrinterCombinators">Printer Combinators</h1>
<p>
This module provides combinators to build printers for user-defined types.
It doesn't try to do <b>pretty</b>-printing (see for instance Pprint for this),
but a simple way to print complicated values without writing a lot of code.
<p>
Those combinators work with "%a". For instance to print a
<code class="code">(int * bool) list list</code> and a <code class="code">float array</code>, one can write:
<pre class="codepre"><code class="code">&nbsp;&nbsp;<span class="constructor">CCPrint</span>.(printf&nbsp;<span class="string">"int:&nbsp;%d&nbsp;list:&nbsp;%a,&nbsp;array:&nbsp;%a\n"</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;42<br>
&nbsp;&nbsp;&nbsp;&nbsp;(list&nbsp;(list&nbsp;(pair&nbsp;int&nbsp;bool)))&nbsp;[[1,&nbsp;<span class="keyword">true</span>;&nbsp;2,&nbsp;<span class="keyword">false</span>];&nbsp;[4,&nbsp;<span class="keyword">true</span>]]<br>
&nbsp;&nbsp;&nbsp;&nbsp;(array&nbsp;float)&nbsp;[|&nbsp;1.&nbsp;;&nbsp;2.&nbsp;;&nbsp;3e18&nbsp;|]&nbsp;;;<br>
</code></pre>
<p>
Remember that "%a" in this context requires two arguments:<ul>
<li>a value of type <code class="code"><span class="keywordsign">'</span>a t</code> (buffer printer)</li>
<li>a value of type <code class="code"><span class="keywordsign">'</span>a</code> (value to print)</li>
</ul>
To define new printers, one can either use existing ones (e.g. <code class="code">list int</code>),
or use <code class="code"><span class="constructor">Printf</span>.bprintf</code>. For instance a printer for colored points in 2D:
<p>
<pre class="codepre"><code class="code">&nbsp;<span class="keyword">type</span>&nbsp;point&nbsp;=&nbsp;{x:int;&nbsp;y:int;&nbsp;colors:&nbsp;string&nbsp;list};;<br>
<br>
<span class="keyword">let</span>&nbsp;pp_point&nbsp;buf&nbsp;p&nbsp;=<br>
&nbsp;&nbsp;<span class="constructor">Printf</span>.bprintf&nbsp;buf&nbsp;<span class="string">"{x=%d,&nbsp;y=%d,&nbsp;colors=%a}"</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;p.x&nbsp;p.y&nbsp;<span class="constructor">CCPrint</span>.(list&nbsp;string)&nbsp;p.colors;;<br>
</code></pre><br>
</div>
<hr width="100%">
<pre><span id="TYPEsequence"><span class="keyword">type</span> <code class="type">'a</code> sequence</span> = <code class="type">('a -> unit) -> unit</code> </pre>
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">'a</code> t</span> = <code class="type">Buffer.t -> 'a -> unit</code> </pre>
<div class="info ">
A printer for the type <code class="code"><span class="keywordsign">'</span>a</code><br>
</div>
<br>
<h2 id="2_Combinators">Combinators</h2><br>
<pre><span id="VALsilent"><span class="keyword">val</span> silent</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a></code></pre><div class="info ">
prints nothing<br>
</div>
<pre><span id="VALunit"><span class="keyword">val</span> unit</span> : <code class="type">unit <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALint"><span class="keyword">val</span> int</span> : <code class="type">int <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALstring"><span class="keyword">val</span> string</span> : <code class="type">string <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALbool"><span class="keyword">val</span> bool</span> : <code class="type">bool <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALfloat3"><span class="keyword">val</span> float3</span> : <code class="type">float <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALfloat"><span class="keyword">val</span> float</span> : <code class="type">float <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALchar"><span class="keyword">val</span> char</span> : <code class="type">char <a href="CCPrint.html#TYPEt">t</a></code></pre><div class="info ">
<b>Since</b> 0.14<br>
</div>
<pre><span id="VALlist"><span class="keyword">val</span> list</span> : <code class="type">?start:string -><br> ?stop:string -> ?sep:string -> 'a <a href="CCPrint.html#TYPEt">t</a> -> 'a list <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALarray"><span class="keyword">val</span> array</span> : <code class="type">?start:string -><br> ?stop:string -> ?sep:string -> 'a <a href="CCPrint.html#TYPEt">t</a> -> 'a array <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALarrayi"><span class="keyword">val</span> arrayi</span> : <code class="type">?start:string -><br> ?stop:string -> ?sep:string -> (int * 'a) <a href="CCPrint.html#TYPEt">t</a> -> 'a array <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALseq"><span class="keyword">val</span> seq</span> : <code class="type">?start:string -><br> ?stop:string -> ?sep:string -> 'a <a href="CCPrint.html#TYPEt">t</a> -> 'a <a href="CCPrint.html#TYPEsequence">sequence</a> <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALopt"><span class="keyword">val</span> opt</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a> -> 'a option <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALpair"><span class="keyword">val</span> pair</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a> -> 'b <a href="CCPrint.html#TYPEt">t</a> -> ('a * 'b) <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALtriple"><span class="keyword">val</span> triple</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a> -> 'b <a href="CCPrint.html#TYPEt">t</a> -> 'c <a href="CCPrint.html#TYPEt">t</a> -> ('a * 'b * 'c) <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALquad"><span class="keyword">val</span> quad</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a> -><br> 'b <a href="CCPrint.html#TYPEt">t</a> -> 'c <a href="CCPrint.html#TYPEt">t</a> -> 'd <a href="CCPrint.html#TYPEt">t</a> -> ('a * 'b * 'c * 'd) <a href="CCPrint.html#TYPEt">t</a></code></pre>
<pre><span id="VALmap"><span class="keyword">val</span> map</span> : <code class="type">('a -> 'b) -> 'b <a href="CCPrint.html#TYPEt">t</a> -> 'a <a href="CCPrint.html#TYPEt">t</a></code></pre><br>
<h2 id="2_IO">IO</h2><br>
<pre><span id="VALoutput"><span class="keyword">val</span> output</span> : <code class="type">Pervasives.out_channel -> 'a <a href="CCPrint.html#TYPEt">t</a> -> 'a -> unit</code></pre>
<pre><span id="VALto_string"><span class="keyword">val</span> to_string</span> : <code class="type">'a <a href="CCPrint.html#TYPEt">t</a> -> 'a -> string</code></pre>
<pre><span id="VALsprintf"><span class="keyword">val</span> sprintf</span> : <code class="type">('a, Buffer.t, unit, string) Pervasives.format4 -> 'a</code></pre><div class="info ">
Print into a string<br>
</div>
<pre><span id="VALfprintf"><span class="keyword">val</span> fprintf</span> : <code class="type">Pervasives.out_channel -> ('a, Buffer.t, unit, unit) Pervasives.format4 -> 'a</code></pre><div class="info ">
Print on a channel<br>
</div>
<pre><span id="VALto_file"><span class="keyword">val</span> to_file</span> : <code class="type">string -> ('a, Buffer.t, unit, unit) Pervasives.format4 -> 'a</code></pre><div class="info ">
Print to the given file<br>
</div>
<pre><span id="VALprintf"><span class="keyword">val</span> printf</span> : <code class="type">('a, Buffer.t, unit, unit) Pervasives.format4 -> 'a</code></pre>
<pre><span id="VALeprintf"><span class="keyword">val</span> eprintf</span> : <code class="type">('a, Buffer.t, unit, unit) Pervasives.format4 -> 'a</code></pre><br>
<h2 id="2_MonadicIO">Monadic IO</h2><br>
<pre><span class="keyword">module type</span> <a href="CCPrint.MONAD_IO.html">MONAD_IO</a> = <code class="code"><span class="keyword">sig</span></code> <a href="CCPrint.MONAD_IO.html">..</a> <code class="code"><span class="keyword">end</span></code></pre>
<pre><span class="keyword">module</span> <a href="CCPrint.MakeIO.html">MakeIO</a> <code class="code">(</code><code class="code"><span class="constructor">M</span></code><code class="code"> : </code><code class="type"><a href="CCPrint.MONAD_IO.html">MONAD_IO</a></code><code class="code">) </code>: <code class="code"><span class="keyword">sig</span></code> <a href="CCPrint.MakeIO.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info">
Example:
<pre class="codepre"><code class="code">&nbsp;<span class="keyword">module</span>&nbsp;<span class="constructor">PrintLwt</span>&nbsp;=&nbsp;<span class="constructor">CCPrint</span>.<span class="constructor">MakeIO</span>(<span class="keyword">struct</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">include</span>&nbsp;<span class="constructor">Lwt</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">type</span>&nbsp;output&nbsp;=&nbsp;<span class="constructor">Lwt_io</span>.output_channel<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;write&nbsp;=&nbsp;<span class="constructor">Lwt_io</span>.write<br>
&nbsp;&nbsp;<span class="keyword">end</span>);;<br>
<br>
&nbsp;&nbsp;<span class="constructor">PrintLwt</span>.printl&nbsp;<span class="constructor">Lwt_io</span>.stdout&nbsp;(<span class="constructor">CCList</span>.pp&nbsp;<span class="constructor">CCInt</span>.pp)&nbsp;[1;2;3;4];;<br>
&nbsp;&nbsp;-&nbsp;:&nbsp;unit&nbsp;<span class="constructor">Lwt</span>.t<br>
</code></pre>
</div>
</body></html>