Getting Started

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

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.

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.

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

$ cd luajit-2.1/
luajit-2.1 $ make -j8
  1. Install LuaJIT

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:

$ 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):

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.

Using git

  1. Clone Lua Fun repository:

git clone git://github.com/luafun/luafun.git
$ cd luafun
  1. Run tests (optional):

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:

$ 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:

 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 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:

 > 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:

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

Now you can use Lua Fun:

> 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 API Reference.

  • Use Index to find functions by its names.

  • Checkout examples from tests/ directory

  • Read 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.