miller/doc/content-for-cookbook2.html
2017-02-20 21:33:10 -05:00

192 lines
7.3 KiB
HTML

<p/>
<center>
<boldmaroon>Random things, and some math</boldmaroon>
</center>
POKI_PUT_TOC_HERE
<p/>
<button style="font-weight:bold;color:maroon;border:0" onclick="expand_all();" href="javascript:;">Expand all sections</button>
<button style="font-weight:bold;color:maroon;border:0" onclick="collapse_all();" href="javascript:;">Collapse all sections</button>
<!-- ================================================================ -->
<h1>Randomly selecting words from a list</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_generating_random_words');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_generating_random_words" style="display: block">
<p/> Given this <a href="data/english-words.txt">word list</a>, first take a
look to see what the first few lines look like:
<p/>
<div class="pokipanel"> <pre>
$ head data/english-words.txt
a
aa
aal
aalii
aam
aardvark
aardwolf
aba
abac
abaca
</pre> </div>
<p/> Then the following will randomly sample ten words with four to eight characters in them:
<p/>
<div class="pokipanel"> <pre>
$ mlr --from data/english-words.txt --nidx filter 'n=strlen($1);4<=n&&n<=8' then sample -k 10
thionine
birchman
mildewy
avigate
addedly
abaze
askant
aiming
insulant
coinmate
</pre> </div>
</div>
<!-- ================================================================ -->
<h1>Randomly generating jabberwocky words</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_jabberwocky');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_jabberwocky" style="display: block">
<p/> These are simple <i>n</i>-grams as described
<a href="http://johnkerl.org/randspell/randspell-slides-ts.pdf">here</a>. Some common functions are
<a href="ngrams/ngfuncs.mlr.txt">here</a>.
Then here are scripts for
<a href="ngrams/ng1.mlr.txt">1-grams</a>,
<a href="ngrams/ng2.mlr.txt">2-grams</a>,
<a href="ngrams/ng3.mlr.txt">3-grams</a>,
<a href="ngrams/ng4.mlr.txt">4-grams</a>, and
<a href="ngrams/ng5.mlr.txt">5-grams</a>.
<p/>The idea is that words from the input file are consumed, then taken apart and pasted back together
in ways which imitate the letter-to-letter transitions found in the word list &mdash; giving us automatically
generated words in the same vein as <i>bromance</i> and <i>spork</i>:
<p/>
<div class="pokipanel"> <pre>
$ mlr --nidx --from ./ngrams/gsl-2000.txt put -q -f ./ngrams/ngfuncs.mlr -f ./ngrams/ng5.mlr
beard
plastinguish
politicially
noise
loan
country
controductionary
suppery
lose
lessors
dollar
judge
rottendence
lessenger
diffendant
suggestional
</pre> </div>
</div>
<!-- ================================================================ -->
<h1>Program timing</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_program_timing');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_program_timing" style="display: block">
This admittedly artificial example demonstrates using Miller time and stats
functions to introspectively acquire some information about Miller&rsquo;s own
runtime. The <tt>delta</tt> function computes the difference between successive
timestamps.
POKI_INCLUDE_ESCAPED(data/timing-example.txt)HERE
</div>
<!-- ================================================================ -->
<h1>Generating random numbers from various distributions</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_generating_random_variables');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_generating_random_variables" style="display: block">
<p/>Here we can chain together a few simple building blocks:
POKI_RUN_COMMAND{{cat expo-sample.sh}}HERE
<p/>Namely:
<ul>
<li/> Set the Miller random-number seed so this webdoc looks the same every time I regenerate it.
<li/> Use pretty-printed tabular output.
<li/> Use pretty-printed tabular output.
<li/> Use <tt>seqgen</tt> to produce 100,000 records <tt>i=0</tt>, <tt>i=1</tt>, etc.
<li/> Send those to a <tt>put</tt> step which defines an inverse-transform-sampling function and
calls it twice, then computes the sum and product of samples.
<li/> Send those to a histogram, and from there to a bar-plotter. This is just for visualization; you
could just as well output CSV and send that off to your own plotting tool, etc.
</ul>
The output is as follows:
POKI_RUN_COMMAND{{sh expo-sample.sh}}HERE
</div>
<!-- ================================================================ -->
<h1>Sieve of Eratosthenes</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_sieve');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_sieve" style="display: block">
<p/> The <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve_of_Eratosthenes</a>
is a standard introductory programming topic. The idea is to find all primes up
to some <i>N</i> by making a list of the numbers 1 to <i>N</i>, then striking
out all multiples of 2 except 2 itself, all multiples of 3 except 3 itself, all
multiples of 4 except 4 itself, and so on. Whatever survives that without
getting marked is a prime. This is easy enough in Miller. Notice that here all
the work is in <tt>begin</tt> and <tt>end</tt> statements; there is no file
input (so we use <tt>mlr -n</tt> to keep Miller from waiting for input data).
POKI_RUN_COMMAND{{cat programs/sieve.mlr}}HERE
POKI_RUN_COMMAND{{mlr -n put -f programs/sieve.mlr}}HERE
</div>
<!-- ================================================================ -->
<h1>Mandelbrot-set generator</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_mand');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_mand" style="display: block">
<p/> The <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot
set</a> is also easily expressed. This isn&rsquo;t an important case of
data-processing in the vein for which Miller was designed, but it is an
example of Miller as a general-purpose programming language &mdash; a test
case for the expressiveness of the language.
<p/> The (approximate) computation of points in the complex plane which are and
aren&rsquo;t members is just a few lines of complex arithmetic (see the
Wikipedia article); how to render them is another task. Using graphics
libraries you can create PNG or JPEG files, but another fun way to do this is
by printing various characters to the screen:
POKI_RUN_COMMAND{{cat programs/mand.mlr}}HERE
<p/> At standard resolution this makes a nice little ASCII plot:
POKI_RUN_COMMAND{{mlr -n put -f ./programs/mand.mlr}}HERE
<p/> But using a very small font size (as small as my Mac will let me go), and by choosing the
coordinates to zoom in on a particular part of the complex plane, we can get a nice little
picture:
<div class=pokipanel> <pre>
#!/bin/bash
# Get the number of rows and columns from the terminal window dimensions
iheight=$(stty size | mlr --nidx --fs space cut -f 1)
iwidth=$(stty size | mlr --nidx --fs space cut -f 2)
echo "rcorn=-1.755350,icorn=+0.014230,side=0.000020,maxits=10000,iheight=$iheight,iwidth=$iwidth" \
| mlr put -f programs/mand.mlr
</pre></div>
<center>
<img src="pix/mand.png"/>
</center>
</div>