mirror of
https://github.com/johnkerl/miller.git
synced 2026-01-24 02:36:15 +00:00
303 lines
No EOL
15 KiB
HTML
303 lines
No EOL
15 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Programming-language examples — Miller 6.0.0-alpha documentation</title>
|
||
|
||
<link rel="stylesheet" href="_static/scrolls.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/print.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
|
||
<script src="_static/jquery.js"></script>
|
||
<script src="_static/underscore.js"></script>
|
||
<script src="_static/doctools.js"></script>
|
||
<script src="_static/language_data.js"></script>
|
||
<script src="_static/theme_extras.js"></script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="Miscellaneous examples" href="misc-examples.html" />
|
||
<link rel="prev" title="DKVP I/O examples" href="dkvp-examples.html" />
|
||
</head><body>
|
||
<div id="content">
|
||
<div class="header">
|
||
<h1 class="heading"><a href="index.html"
|
||
title="back to the documentation overview"><span>Programming-language examples</span></a></h1>
|
||
</div>
|
||
<div class="relnav" role="navigation" aria-label="related navigation">
|
||
<a href="dkvp-examples.html">« DKVP I/O examples</a> |
|
||
<a href="#">Programming-language examples</a>
|
||
| <a href="misc-examples.html">Miscellaneous examples »</a>
|
||
</div>
|
||
<div id="contentwrapper">
|
||
<div id="toc" role="navigation" aria-label="table of contents navigation">
|
||
<h3>Table of Contents</h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">Programming-language examples</a><ul>
|
||
<li><a class="reference internal" href="#sieve-of-eratosthenes">Sieve of Eratosthenes</a></li>
|
||
<li><a class="reference internal" href="#mandelbrot-set-generator">Mandelbrot-set generator</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
<div role="main">
|
||
|
||
<div class="section" id="programming-language-examples">
|
||
<h1>Programming-language examples<a class="headerlink" href="#programming-language-examples" title="Permalink to this headline">¶</a></h1>
|
||
<p>Here are a few things focusing on Miller’s DSL as a programming language per se, outside of its normal use for streaming record-processing.</p>
|
||
<div class="section" id="sieve-of-eratosthenes">
|
||
<h2>Sieve of Eratosthenes<a class="headerlink" href="#sieve-of-eratosthenes" title="Permalink to this headline">¶</a></h2>
|
||
<p>The <a class="reference external" 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 <em>N</em> by making a list of the numbers 1 to <em>N</em>, 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 <code class="docutils literal notranslate"><span class="pre">begin</span></code> and <code class="docutils literal notranslate"><span class="pre">end</span></code> statements; there is no file input (so we use <code class="docutils literal notranslate"><span class="pre">mlr</span> <span class="pre">-n</span></code> to keep Miller from waiting for input data).</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll"> cat programs/sieve.mlr
|
||
</span> # ================================================================
|
||
# Sieve of Eratosthenes: simple example of Miller DSL as programming language.
|
||
# ================================================================
|
||
|
||
# Put this in a begin-block so we can do either
|
||
# mlr -n put -q -f name-of-this-file.mlr
|
||
# or
|
||
# mlr -n put -q -f name-of-this-file.mlr -e '@n = 200'
|
||
# i.e. 100 is the default upper limit, and another can be specified using -e.
|
||
begin {
|
||
@n = 100;
|
||
}
|
||
|
||
end {
|
||
for (int i = 0; i <= @n; i += 1) {
|
||
@s[i] = true;
|
||
}
|
||
@s[0] = false; # 0 is neither prime nor composite
|
||
@s[1] = false; # 1 is neither prime nor composite
|
||
# Strike out multiples
|
||
for (int i = 2; i <= @n; i += 1) {
|
||
for (int j = i+i; j <= @n; j += i) {
|
||
@s[j] = false;
|
||
}
|
||
}
|
||
# Print survivors
|
||
for (int i = 0; i <= @n; i += 1) {
|
||
if (@s[i]) {
|
||
print i;
|
||
}
|
||
}
|
||
}
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll"> mlr -n put -f programs/sieve.mlr
|
||
</span> 2
|
||
3
|
||
5
|
||
7
|
||
11
|
||
13
|
||
17
|
||
19
|
||
23
|
||
29
|
||
31
|
||
37
|
||
41
|
||
43
|
||
47
|
||
53
|
||
59
|
||
61
|
||
67
|
||
71
|
||
73
|
||
79
|
||
83
|
||
89
|
||
97
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="mandelbrot-set-generator">
|
||
<h2>Mandelbrot-set generator<a class="headerlink" href="#mandelbrot-set-generator" title="Permalink to this headline">¶</a></h2>
|
||
<p>The <a class="reference external" href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot set</a> is also easily expressed. This isn’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 – a test case for the expressiveness of the language.</p>
|
||
<p>The (approximate) computation of points in the complex plane which are and aren’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:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll"> cat programs/mand.mlr
|
||
</span> # Mandelbrot set generator: simple example of Miller DSL as programming language.
|
||
begin {
|
||
# Set defaults
|
||
@rcorn = -2.0;
|
||
@icorn = -2.0;
|
||
@side = 4.0;
|
||
@iheight = 50;
|
||
@iwidth = 100;
|
||
@maxits = 100;
|
||
@levelstep = 5;
|
||
@chars = "@X*o-."; # Palette of characters to print to the screen.
|
||
@verbose = false;
|
||
@do_julia = false;
|
||
@jr = 0.0; # Real part of Julia point, if any
|
||
@ji = 0.0; # Imaginary part of Julia point, if any
|
||
}
|
||
|
||
# Here, we can override defaults from an input file (if any). In Miller's
|
||
# put/filter DSL, absent-null right-hand sides result in no assignment so we
|
||
# can simply put @rcorn = $rcorn: if there is a field in the input like
|
||
# 'rcorn = -1.847' we'll read and use it, else we'll keep the default.
|
||
@rcorn = $rcorn;
|
||
@icorn = $icorn;
|
||
@side = $side;
|
||
@iheight = $iheight;
|
||
@iwidth = $iwidth;
|
||
@maxits = $maxits;
|
||
@levelstep = $levelstep;
|
||
@chars = $chars;
|
||
@verbose = $verbose;
|
||
@do_julia = $do_julia;
|
||
@jr = $jr;
|
||
@ji = $ji;
|
||
|
||
end {
|
||
if (@verbose) {
|
||
print "RCORN = ".@rcorn;
|
||
print "ICORN = ".@icorn;
|
||
print "SIDE = ".@side;
|
||
print "IHEIGHT = ".@iheight;
|
||
print "IWIDTH = ".@iwidth;
|
||
print "MAXITS = ".@maxits;
|
||
print "LEVELSTEP = ".@levelstep;
|
||
print "CHARS = ".@chars;
|
||
}
|
||
|
||
# Iterate over a matrix of rows and columns, printing one character for each cell.
|
||
for (int ii = @iheight-1; ii >= 0; ii -= 1) {
|
||
num pi = @icorn + (ii/@iheight) * @side;
|
||
for (int ir = 0; ir < @iwidth; ir += 1) {
|
||
num pr = @rcorn + (ir/@iwidth) * @side;
|
||
printn get_point_plot(pr, pi, @maxits, @do_julia, @jr, @ji);
|
||
}
|
||
print;
|
||
}
|
||
}
|
||
|
||
# This is a function to approximate membership in the Mandelbrot set (or Julia
|
||
# set for a given Julia point if do_julia == true) for a given point in the
|
||
# complex plane.
|
||
func get_point_plot(pr, pi, maxits, do_julia, jr, ji) {
|
||
num zr = 0.0;
|
||
num zi = 0.0;
|
||
num cr = 0.0;
|
||
num ci = 0.0;
|
||
|
||
if (!do_julia) {
|
||
zr = 0.0;
|
||
zi = 0.0;
|
||
cr = pr;
|
||
ci = pi;
|
||
} else {
|
||
zr = pr;
|
||
zi = pi;
|
||
cr = jr;
|
||
ci = ji;
|
||
}
|
||
|
||
int iti = 0;
|
||
bool escaped = false;
|
||
num zt = 0;
|
||
for (iti = 0; iti < maxits; iti += 1) {
|
||
num mag = zr*zr + zi+zi;
|
||
if (mag > 4.0) {
|
||
escaped = true;
|
||
break;
|
||
}
|
||
# z := z^2 + c
|
||
zt = zr*zr - zi*zi + cr;
|
||
zi = 2*zr*zi + ci;
|
||
zr = zt;
|
||
}
|
||
if (!escaped) {
|
||
return ".";
|
||
} else {
|
||
# The // operator is Miller's (pythonic) integer-division operator
|
||
int level = (iti // @levelstep) % strlen(@chars);
|
||
return substr(@chars, level, level);
|
||
}
|
||
}
|
||
</pre></div>
|
||
</div>
|
||
<p>At standard resolution this makes a nice little ASCII plot:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll"> mlr -n put -f ./programs/mand.mlr
|
||
</span> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
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@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@@@XXXXXXXXXX**oo*-.-........oo.XXXXXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@@@@@@XXXXXXXXXXXXX....................X..o-XXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@@@XXXXXXXXXXXXXXX*oo......................oXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@@@XXX*XXXXXXXXXXXX**o........................*X*X@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@XXXXXXooo***o*.*XX**X..........................o-XX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@XXXXXXXX*-.......-***.............................oXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@XXXXXXXX*@..........Xo............................*XX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@XXXX@XXXXXXXX*o@oX...........@...........................oXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
.........................................................o*XXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@XXXXXXXXX*-.oX...........@...........................oXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@XXXXXXXXXX**@..........*o............................*XXXXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@XXXXXXXXXXXXX-........***.............................oXXXXXXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@XXXXXXXXXXXXoo****o*.XX***@..........................o-XXXXXXXXXXXXX@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
@@@@@@@@@@@@@@XXXXX*X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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
</pre></div>
|
||
</div>
|
||
<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:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#!/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>
|
||
</div>
|
||
<img alt="_images/mand.png" src="_images/mand.png" />
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="footer" role="contentinfo">
|
||
© Copyright 2021, John Kerl.
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
|
||
</div>
|
||
</body>
|
||
</html> |