summaryrefslogtreecommitdiff
path: root/Data/DefaultContent/Libraries/luafun/doc/getting_started.rst
blob: 9a9e3dee5f5c12503f844aa0508c80b93d4281bb (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
Getting Started
===============

Please jump to `Using the Library`_ section if you are familiar with Lua and
LuaJIT.

.. contents::

Prerequisites
-------------

The library is designed for LuaJIT_. **LuaJIT 2.1 alpha** is high^W **Highly**
recommended for performance reasons. Lua 5.1--5.3 are also supported.

The library is platform-independent and expected to work on all platforms that
supported by Lua(JIT). It can be also used in any Lua(JIT) based applications,
e.g. Tarantool_ or OpenResty_.

You might need diff_ tool to run test system and sphinx_ to regenerate the
documentation from source files.

.. _LuaJIT: http://luajit.org/
.. _Tarantool: http://tarantool.org/
.. _OpenResty: http://openresty.org/
.. _diff: http://en.wikipedia.org/wiki/Diff
.. _sphinx: http://sphinx-doc.org/

Installing LuaJIT
-----------------

You can build LuaJIT from sources or install it from a binary archive.

From Sources
````````````

1. Clone LuaJIT git repository. Please note that **v2.1** branch is needed.
You can always select this branch using ``git checkout v2.1``.

.. code-block:: bash

    $ git clone http://luajit.org/git/luajit-2.0.git -b v2.1 luajit-2.1
    Cloning into 'luajit-2.1'...

2. Compile LuaJIT

.. code-block:: bash

    $ cd luajit-2.1/
    luajit-2.1 $ make -j8

3. Install LuaJIT

.. code-block:: bash

    luajit-2.1 $ make install
    luajit-2.1 $ ln -s /usr/local/bin/luajit-2.1.0-alpha /usr/local/bin/luajit

Install operation might require root permissions. However, you can install
LuaJIT into your home directory.

From a Binary Archive
`````````````````````

If operations above look too complicated for you, you always can download a
binary archive from http://luajit.org/download.html page.
Your favorite package manager may also have LuaJIT packages.

Running LuaJIT
``````````````

Ensure that freshly installed LuaJIT works:

.. code-block:: bash

    $ luajit
    LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
    JIT: ON SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
    > = 2 + 2
    4

It is good idea to use LuaJIT CLI under ``rlwrap`` (on nix platforms):

.. code-block:: bash

    alias luajit="rlwrap luajit"
    $ luajit
    LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
    JIT: ON SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
    > = 2 + 2
    4
    > = 2 + 2 <!-- You can use arrows, completion and so on, like in Bash

Installing the Library
----------------------

Using LuaRocks
``````````````

Use the rockspec_ file.

.. _rockspec: https://raw.github.com/luafun/luafun/master/fun-scm-1.rockspec

Using git
`````````
1. Clone Lua Fun repository:

.. code-block:: bash

    git clone git://github.com/luafun/luafun.git
    $ cd luafun

2. Run tests (optional):

.. code-block:: bash

    luafun $ cd tests
    luafun/tests $ ./runtest *.lua
    Testing basic.lua
    Testing compositions.lua
    Testing filters.lua
    Testing folds.lua
    Testing generators.lua
    Testing slices.lua
    Testing transformations.lua
    All tests have passed!

Using wget
``````````

Just download https://raw.github.com/luafun/luafun/master/fun.lua file:

.. code-block:: bash

    $ wget https://raw.github.com/luafun/luafun/master/fun.lua

Using the Library
-----------------

Try to run LuaJIT in the same directory where ``fun.lua`` file is located:

.. code-block:: bash
   :emphasize-lines: 4

    luafun $ luajit
    LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
    JIT: ON SSE2 SSE3 fold cse dce fwd dse narrow loop abc sink fuse
    > fun = require 'fun'
    >
    > for _k, a in fun.range(3) do print(a) end
    1
    2
    3

If you see an error message like ``stdin:1: module 'fun' not found:`` then
you need to configure you Package Path (``package.path``). Please consult
`Lua Wiki <http://lua-users.org/wiki/PackagePath>`_ for additional information.


**Lua Fun** designed to be small ubiquitous library. It is a good idea to import
all library functions to the global table:

.. code-block:: bash
   :emphasize-lines: 1

    > for k, v in pairs(require "fun") do _G[k] = v end -- import fun.*
    > for _k, a in range(3) do print(a) end
    0
    1
    2

**Lua Fun** also provides a special **shortcut** to autoimport all functions:

.. code-block:: bash
   :emphasize-lines: 1

    > require 'fun'() -- to import all lua.* functions to globals
    > each(print, range(5))
    1
    2
    3
    4
    5

Now you can use **Lua Fun**:

.. code-block:: bash

    > print(sum(filter(function(x) return x % 16 == 0 end, range(10000))))
    3130000

    > each(print, take(5, tabulate(math.sin)))
    0
    2
    4
    6
    8

    > each(print, enumerate(zip({"one", "two", "three", "four", "five"},
        {"a", "b", "c", "d", "e"})))
    1       one     a
    2       two     b
    3       three   c
    4       four    d
    5       five    e

    > lines_to_grep = {
        [[Emily]],
        [[Chloe]],
        [[Megan]],
        [[Jessica]],
        [[Emma]],
        [[Sarah]],
        [[Elizabeth]],
        [[Sophie]],
        [[Olivia]],
        [[Lauren]]
    }

    > each(print, grep("Em", lines_to_grep))
    Emily
    Emma

    > each(print, take(10, cycle(chain(
        {enumerate({"a", "b", "c"})},
        {"one", "two", "three"}))
      ))
    0 a
    1 b
    2 c
    one
    two
    three
    0 a
    1 b
    2 c
    one

Please note that functions support multireturn.

Further Actions
---------------

- Take a look on :doc:`reference`.
- Use :ref:`genindex` to find functions by its names.
- Checkout **examples** from
  `tests/ <https://github.com/luafun/luafun/tree/master/tests>`_ directory
- Read :doc:`under_the_hood` section
- "Star" us the on GitHub_ to help the project to survive
- Make Great Software
- Have fun

**Lua Fun**. Simple, Efficient and Functional. In Lua. With JIT.

.. _GitHub: http://github.com/luafun/luafun