roul/usage/index.html @ 10f91124d5a9

clojure-lanterna: Update site.
author Steve Losh <steve@stevelosh.com>
date Thu, 12 Jul 2012 22:39:19 -0400
parents 3f3e2c21221e
children 9c78e1a01787
<html><head><title>Usage / Roul</title><link rel="stylesheet" href="../_dmedia/bootstrap.css"/><link rel="stylesheet" href="../_dmedia/tango.css"/><link rel="stylesheet/less" type="text/css" href="../_dmedia/style.less"/><script src="../_dmedia/less.js" type="text/javascript">
        </script></head><body class="content">
        <div class="wrap">
            <header><h1><a href="..">Roul</a></h1></header><div class="markdown">
<h1 id="usage"><a href="">Usage</a></h1><p>Roul currently contains a few functions in one namespace.  It'll expand in the
future, but for now that's all it is.</p>
<div class="toc">
<ul><li><a href="#roulrandom">roul.random</a><ul><li><a href="#rand">rand</a></li>
<li><a href="#rand-int">rand-int</a></li>
<li><a href="#rand-nth">rand-nth</a></li>
<li><a href="#rand-nth-weighted">rand-nth-weighted</a></li>
</ul></li>
</ul></div>
<h2 id="roulrandom">roul.random</h2>
<p>The <code>roul.random</code> namespace contains wrappers around some of Clojure's built-in
random functions to make them more user friendly, as well as some uniqe
functions of its own.</p>
<p>The recommended way is to <code>require</code> this namespace into your own instead of
overwriting Clojure's builtins:</p>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">ns</span> <span class="nv">foo</span><span class="o">.</span><span class="nv">core</span>
  <span class="p">(</span><span class="nf">:require</span> <span class="p">[</span><span class="nv">roul</span><span class="o">.</span><span class="nv">random</span> <span class="nv">:as</span> <span class="nv">rr</span><span class="p">]))</span>

<span class="c1">; or</span>

<span class="p">(</span><span class="nf">require</span> <span class="o">'</span><span class="p">[</span><span class="nv">roul</span><span class="o">.</span><span class="nv">random</span> <span class="nv">:as</span> <span class="nv">rr</span><span class="p">])</span>
</pre></div>


<h3 id="rand">rand</h3>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">rand</span><span class="p">)</span>           <span class="c1">; return a float in [0, 1)</span>
<span class="p">(</span><span class="nb">rand </span><span class="nv">end</span><span class="p">)</span>       <span class="c1">; return a float in [0, end)</span>
<span class="p">(</span><span class="nb">rand </span><span class="nv">start</span> <span class="nv">end</span><span class="p">)</span> <span class="c1">; return a float in [start, end)</span>
</pre></div>


<p>A wrapper around the built-in <code>rand</code>.  Returns a random floating point number
between <code>start</code> (inclusive) and <code>end</code> (exclusive).</p>
<p>If not given, <code>start</code> defaults to <code>0</code> and <code>end</code> defaults to <code>1</code>.</p>
<h3 id="rand-int">rand-int</h3>
<div class="codehilite"><pre><span class="p">(</span><span class="nb">rand-int </span><span class="nv">end</span><span class="p">)</span>       <span class="c1">; return an int in [0, end)</span>
<span class="p">(</span><span class="nb">rand-int </span><span class="nv">start</span> <span class="nv">end</span><span class="p">)</span> <span class="c1">; return an int in [start, end)</span>
</pre></div>


<p>A wrapper around the built-in <code>rand-int</code>.  Returns a random integer between
<code>start</code> (inclusive) and <code>end</code> (exclusive).</p>
<p>If not given, <code>start</code> defaults to <code>0</code>.</p>
<h3 id="rand-nth">rand-nth</h3>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">rand-nth</span> <span class="nv">coll</span><span class="p">)</span> <span class="c1">; return a random element of coll</span>
</pre></div>


<p>A wrapper around the built-in <code>rand-nth</code>.  Returns a random element of the given
collection.</p>
<p>This is a transparent wrapper around the builtin, included only for
completeness.  Unlike the other wrappers no new functionality has been added
(yet).</p>
<h3 id="rand-nth-weighted">rand-nth-weighted</h3>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">rand-nth-weighted</span> <span class="nv">coll</span><span class="p">)</span> <span class="c1">; return a random element of coll</span>
</pre></div>


<p>Returns a random element of a weighted collection.</p>
<p>A weighted collection can be any seq of <code>[choice, weight]</code> elements.  The
weights can be arbitrary numbers -- they do not need to add up to anything
specific.</p>
<div class="codehilite"><pre><span class="c1">; Returns coffee roughly 80% of the time, tea 15%, and soda 5%.</span>
<span class="p">(</span><span class="nf">rr/rand-nth-weighted</span> <span class="p">{</span><span class="nv">:coffee</span> <span class="mf">0.80</span><span class="o">,</span> <span class="nv">:tea</span> <span class="mf">0.15</span><span class="o">,</span> <span class="nv">:soda</span> <span class="mf">0.05</span><span class="p">})</span>

<span class="c1">; Returns cats roughly twice as often as boots.</span>
<span class="p">(</span><span class="nf">rr/rand-nth-weighted</span> <span class="p">[[</span><span class="nv">:boots</span> <span class="mi">14</span><span class="p">]</span>
                       <span class="p">[</span><span class="nv">:cats</span> <span class="mi">28</span><span class="p">]])</span>
</pre></div>
                </div>
            <footer><p>Created by <a href="http://stevelosh.com">Steve Losh</a>.
Docs made with <a href="http://sjl.bitbucket.org/d/">d</a>.</p>
<p><br/><a id="rochester-made" href="http://rochestermade.com" title="Rochester Made"><img src="http://rochestermade.com/media/images/rochester-made-dark-on-light.png" alt="Rochester Made" title="Rochester Made"/></a></p>
<script type="text/javascript">
  var _gauges = _gauges || [];
  (function() {
    var t   = document.createElement('script');
    t.type  = 'text/javascript';
    t.async = true;
    t.id    = 'gauges-tracker';
    t.setAttribute('data-site-id', '4f843f8c613f5d65280000e6');
    t.src = '//secure.gaug.es/track.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(t, s);
  })();
</script></footer></div>
    </body></html>