-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro.html
More file actions
125 lines (110 loc) · 7.9 KB
/
Copy pathintro.html
File metadata and controls
125 lines (110 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Introduction — Lua Functional 0.1.3 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/haiku.css" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Getting Started" href="getting_started.html" />
<link rel="prev" title="Lua Functional Library" href="index.html" />
</head><body>
<div class="header" role="banner"><img class="rightlogo" src="_static/logo.png" alt="Logo"/><h1 class="heading"><a href="index.html">
<span>Lua Functional 0.1.3 documentation</span></a></h1>
<h2 class="heading"><span>Introduction</span></h2>
</div>
<div class="topnav" role="navigation" aria-label="top navigation">
<p>
«  <a href="index.html">Lua Functional Library</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="getting_started.html">Getting Started</a>  »
</p>
</div>
<div class="content" role="main">
<section id="introduction">
<h1>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h1>
<p><strong>Lua Fun</strong> is a high-performance functional programming library
designed for <a class="reference external" href="http://luajit.org/luajit.html">LuaJIT tracing just-in-time compiler</a>.</p>
<p>The library provides a set of more than 50 programming primitives typically
found in languages like Standard ML, Haskell, Erlang, JavaScript, Python and
even Lisp. High-order functions such as <a class="reference internal" href="transformations.html#fun.map" title="fun.map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>, <a class="reference internal" href="filtering.html#fun.filter" title="fun.filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a>,
<a class="reference internal" href="reducing.html#fun.reduce" title="fun.reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code></a>, <a class="reference internal" href="compositions.html#fun.zip" title="fun.zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> will help you to <strong>write simple and efficient
functional code</strong>.</p>
<p>Let’s see an example:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span> <span class="c1">-- Functional style</span>
<span class="hll"> <span class="nb">require</span> <span class="s2">"fun"</span> <span class="p">()</span>
</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">100</span>
<span class="hll"> <span class="n">x</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span><span class="o">^</span><span class="mi">2</span> <span class="kr">end</span><span class="p">,</span> <span class="n">take</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">tabulate</span><span class="p">(</span><span class="nb">math.sin</span><span class="p">))))</span>
</span> <span class="c1">-- calculate sum(sin(x)^2 for x in 0..n-1)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mf">50.011981355266</span>
</pre></div>
</div>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span> <span class="c1">-- Object-oriented style</span>
<span class="hll"> <span class="kd">local</span> <span class="n">fun</span> <span class="o">=</span> <span class="nb">require</span> <span class="s2">"fun"</span>
</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">100</span>
<span class="hll"> <span class="n">x</span> <span class="o">=</span> <span class="n">fun</span><span class="p">.</span><span class="n">tabulate</span><span class="p">(</span><span class="nb">math.sin</span><span class="p">):</span><span class="n">take</span><span class="p">(</span><span class="n">n</span><span class="p">):</span><span class="n">map</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span><span class="o">^</span><span class="mi">2</span> <span class="kr">end</span><span class="p">):</span><span class="n">sum</span><span class="p">()</span>
</span> <span class="c1">-- calculate sum(sin(x)^2 for x in 0..n-1)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mf">50.011981355266</span>
</pre></div>
</div>
<p><strong>Lua Fun</strong> takes full advantage of the innovative <strong>tracing JIT compiler</strong>
to achieve transcendental performance on nested functional expressions.
Functional compositions and high-order functions can be translated into
<strong>efficient machine code</strong>. Can you believe it? Just try to run the example above
with <code class="docutils literal notranslate"><span class="pre">luajit</span> <span class="pre">-jdump</span></code> and see what happens:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span> -- skip some initialization code --
<span class="hll"> ->LOOP:
</span> 0bcaffd0 movsd [rsp+0x8], xmm7
0bcaffd6 addsd xmm4, xmm5
0bcaffda ucomisd xmm6, xmm1
0bcaffde jnb 0x0bca0028 ->6
0bcaffe4 addsd xmm6, xmm0
0bcaffe8 addsd xmm7, xmm0
0bcaffec fld qword [rsp+0x8]
0bcafff0 fsin
0bcafff2 fstp qword [rsp]
0bcafff5 movsd xmm5, [rsp]
0bcafffa mulsd xmm5, xmm5
<span class="hll"> 0bcafffe jmp 0x0bcaffd0 ->LOOP
</span> ---- TRACE 1 stop -> loop
</pre></div>
</div>
<p>The functional chain above was translated by LuaJIT to (!) <strong>one machine loop</strong>
containing just 10 CPU assembly instructions without CALL. Unbelievable!</p>
<p>Readable? Efficient? Can your Python/Ruby/V8 do better?</p>
</section>
</div>
<div class="bottomnav" role="navigation" aria-label="bottom navigation">
<p>
«  <a href="index.html">Lua Functional Library</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="getting_started.html">Getting Started</a>  »
</p>
</div>
<div class="footer" role="contentinfo">
© Copyright 2013-2021, Roman Tsisyk.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.4.0.
</div>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45899190-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>