You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sugar/doc/index.html

6 lines
221 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 years ago
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>Sugar: readability &amp; convenience library</title><link rel="stylesheet" type="text/css" href="scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="racket.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-racket.css" title="default"/><script type="text/javascript" src="scribble-common.js"></script><script type="text/javascript" src="manual-racket.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="scribble-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9658;</a></td><td></td><td><a href="" class="tocviewselflink" data-pltdoc="x">Sugar:<span class="mywbr"> &nbsp;</span> readability &amp; convenience library</a></td></tr></table></div><div class="tocviewsublistonly" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="#%28part._.Installation___updates%29" class="tocviewlink" data-pltdoc="x">Installation &amp; updates</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="#%28part._.Coercion%29" class="tocviewlink" data-pltdoc="x">Coercion</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="#%28part._.Container%29" class="tocviewlink" data-pltdoc="x">Container</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="#%28part._.Debug%29" class="tocviewlink" data-pltdoc="x">Debug</a></td></tr><tr><td align="right">5&nbsp;</td><td><a href="#%28part._.File%29" class="tocviewlink" data-pltdoc="x">File</a></td></tr><tr><td align="right">6&nbsp;</td><td><a href="#%28part._.Len%29" class="tocviewlink" data-pltdoc="x">Len</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="#%28part._.List%29" class="tocviewlink" data-pltdoc="x">List</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="#%28part._.String%29" class="tocviewlink" data-pltdoc="x">String</a></td></tr><tr><td align="right">9&nbsp;</td><td><a href="#%28part._.License___source_code%29" class="tocviewlink" data-pltdoc="x">License &amp; source code</a></td></tr></table></div></div></div><div class="tocsub"><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber"></span><a href="#%28part._.Sugar__readability___convenience_library%29" class="tocsubseclink" data-pltdoc="x">Sugar:<span class="mywbr"> &nbsp;</span> readability &amp; convenience library</a></td></tr><tr><td><span class="tocsublinknumber">1<tt>&nbsp;</tt></span><a href="#%28part._.Installation___updates%29" class="tocsubseclink" data-pltdoc="x">Installation &amp; updates</a></td></tr><tr><td><span class="tocsublinknumber">2<tt>&nbsp;</tt></span><a href="#%28part._.Coercion%29" class="tocsubseclink" data-pltdoc="x">Coercion</a></td></tr><tr><td><span class="tocsublinknumber">2.1<tt>&nbsp;</tt></span><a href="#%28part._.Values%29" class="tocsubseclink" data-pltdoc="x">Values</a></td></tr><tr><td><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink"><span class="nobreak">-<wbr></wbr>&gt;</span>int</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink"><span class="nobreak">-<wbr></wbr>&gt;</span>string</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3esymbol%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink"><span class="nobreak">-<wbr></wbr>&gt;</span>symbol</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._sug
10 years ago
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0"><tr><td><p><span class="stt">raco pkg install sugar</span></p></td></tr></table></div></p><p><div class="SIntrapara">After that, you can update the package from the command line:
10 years ago
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0"><tr><td><p><span class="stt">raco pkg update sugar</span></p></td></tr></table></div></p><h3>2<tt>&nbsp;</tt><a name="(part._.Coercion)"></a><a name="(mod-path._sugar/coerce)"></a>Coercion</h3><p><table cellspacing="0" cellpadding="0" class="defmodule"><tr><td align="left"><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29" class="RktStxLink" data-pltdoc="x">require</a></span><span class="stt"> </span><a href="#%28mod-path._sugar%2Fcoerce%29" class="RktModLink" data-pltdoc="x"><span class="RktSym">sugar/coerce</span></a><span class="RktPn">)</span></td><td align="right"><span class="RpackageSpec"><span class="Smaller">&nbsp;package:</span> <span class="stt">sugar</span></span></td></tr></table></p><p>Functions that coerce the datatype of a value to another type. Racket already has type-specific conversion functions. But if you&rsquo;re handling values of indeterminate type &#8212; as sometimes happens in an untyped language &#8212;&#160;then handling the possible cases individually gets to be a drag.</p><h4>2.1<tt>&nbsp;</tt><a name="(part._.Values)"></a>Values</h4><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._sugar/coerce..rkt)._-~3eint))"></a><span title="Provided from: sugar/coerce | Package: sugar"><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="RktValDef RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>int</a></span></span><span class="hspace">&nbsp;</span><span class="RktVar">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span>&rarr;<span class="hspace">&nbsp;</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29" class="RktValLink" data-pltdoc="x">integer?</a></span></p></blockquote></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVar">v</span><span class="hspace">&nbsp;</span>:<span class="hspace">&nbsp;</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Convert <span class="RktVar">v</span> to an integer in the least surprising way, or raise an error if no conversion is possible.</div></p><p>Numbers are rounded down to the nearest integer.</p><p><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><p>Examples:</p></td></tr><tr><td><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>int</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>int</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3.5</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="RktValLink" data-pltdoc
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><p>Example:</p></td></tr><tr><td><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3eint%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>int</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#t</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">Can&#8217;t convert #t to integer</span></p></td></tr></table></blockquote></td></tr></table></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._sugar/coerce..rkt)._-~3estring))"></a><span title="Provided from: sugar/coerce | Package: sugar"><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="RktValDef RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>string</a></span></span><span class="hspace">&nbsp;</span><span class="RktVar">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span>&rarr;<span class="hspace">&nbsp;</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/strings.html#%28def._%28%28quote._~23~25kernel%29._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span></p></blockquote></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVar">v</span><span class="hspace">&nbsp;</span>:<span class="hspace">&nbsp;</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Return the most natural string representation of <span class="RktVar">v</span>, or raise an error if none exists.</div></p><p><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><p>Examples:</p></td></tr><tr><td><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>string</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"string"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"string"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>string</a></span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">symbol</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"symbol"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>string</a></span><span class="hspace">&nbsp;</span><span class="RktVal">98.6</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"98.6"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._-~3estring%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-&gt;</span>string</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/Manipulating_Paths.html#%28def._%
<br/>(first-condition? x) = #t</span></p><p>But be careful &#8212;&#160;in the example below, the result of the <span class="RktSym"><a href="http://docs.racket-lang.org/reference/if.html#%28form._%28%28quote._~23~25kernel%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expression will be skipped in favor of the last expression, which will be the value of <span class="RktVar">x</span>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/if.html#%28form._%28%28quote._~23~25kernel%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._sugar%2Fdebug..rkt%29._report%29%29" class="RktStxLink" data-pltdoc="x">report</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">first-condition?</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">second-condition?</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">one-thing</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">other-thing</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._sugar%2Fdebug..rkt%29._report%29%29" class="RktStxLink" data-pltdoc="x">report</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span></td></tr></table></blockquote><h3>5<tt>&nbsp;</tt><a name="(part._.File)"></a><a name="(mod-path._sugar/file)"></a>File</h3><p><table cellspacing="0" cellpadding="0" class="defmodule"><tr><td align="left"><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29" class="RktStxLink" data-pltdoc="x">require</a></span><span class="stt"> </span><a href="#%28mod-path._sugar%2Ffile%29" class="RktModLink" data-pltdoc="x"><span class="RktSym">sugar/file</span></a><span class="RktPn">)</span></td><td align="right"><span class="RpackageSpec"><span class="Smaller">&nbsp;package:</span> <span class="stt">sugar</span></span></td></tr></table></p><p>File utilities, mostly in the realm of file extensions. These functions don&rsquo;t access the filesystem.</p><p>Arguments that are <span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Fcoerce..rkt%29._pathish~3f%29%29" class="RktValLink" data-pltdoc="x">pathish?</a></span> can take either a string or a path. For clarity below, I&rsquo;ve used strings.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._sugar/file..rkt)._get-ext))"></a><span title="Provided from: sugar/file | Package: sugar"><span class="RktSym"><a href="#%28def._%28%28lib._sugar%2Ffile..rkt%29._get-ext%29%29" class="RktValDef RktValLink" data-pltdoc="x">get-ext</a></span></span><span class="hspace">&nbsp;</span><span class="RktVar">file-path</span><span class="RktPn">)</sp