

Lua Fun: A high-performance functional programming library designed for LuaJIT  - sibman
https://github.com/rtsisyk/luafun

======
alberth
Even though we occassionally talk about Lua on HN, it frankly still doesn't
get enough attention - nor does Mike Pall and his fantastic work on LuaJIT.

Please considering sponsoring [1] LuaJIT so that Mike can continue working on
it.

Lua Fun looks like a wonderful addition to the Lua ecosystem.

[1] [http://luajit.org/sponsors.html](http://luajit.org/sponsors.html)

~~~
pjmlp
> Even though we occassionally talk about Lua on HN, it frankly still doesn't
> get enough attention - nor does Mike Pall and his fantastic work on LuaJIT.

Because most HNers are web developers and care only about the browser.

Lua is really strong in the game developer community.

~~~
saosebastiao
Most? I'd say far from most. I see just as many articles about Go, Scala,
Clojure, etc. as I do Javascript.

~~~
seabrookmx
Yes, but usually in the context of web development as these are common server
side languages for web apps.

pjmpl just meant we don't see a lot of game developer content on here, which
is largely a C++ dominated area (with Lua game scripting becoming a lot more
common).

------
axman6
It's quite interesting to see essencially the ideas used in stream fusion [1]
to produce practically the same results produced in Haskell, avoiding
allocations and often making it much easier for the compiter ro produce
vectorised code. At least that's what it looks like to me, correct me if I'm
wrong.

Edit: [3] has a good exlanation of how stream fusion works, and it should make
it clearer why the linked code appears to be doing the same thing.

see also here[2] for a further paper on stream fusion being used to produce
vectorised code. I've linked to the reddit comments because there's some
useful discussion there too.

[1]
[http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7...](http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7401)

[2]
[http://www.reddit.com/r/haskell/comments/1br0ls/haskell_beat...](http://www.reddit.com/r/haskell/comments/1br0ls/haskell_beats_c_using_generalised_stream_fusion/)

[3] [http://donsbot.wordpress.com/2008/06/04/haskell-as-fast-
as-c...](http://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-
at-a-high-altitude-for-low-level-performance/)

------
markus2012
In case you skipped the article it contains this nugget:

Take this example containing functional composition and higher-order
functions:

reduce(operator.add, 0, map(function(x) return x^2 end, range(n)))

LUA converts this into 10 lines of assembler (2 jumps). Also, the LUA compiler
is directly generating SSE.

Anyone have any good benchmarks comparing simple functional examples in LUA
with Clojure or Common Lisp?

~~~
matthiasl
I know nothing about LUA syntax, so I also have no idea which part of this is
new and which part is ordinary LUA.

Is \-- calculate sum(x for x^2 in 1..n) a comment or is it the output of a
code transform?

~~~
thelinked
"\--" are comments in Lua, also, sorry for being pedantic but its Lua not LUA.
Its Portuguese for moon.

------
tzury
Underscore Lua is a lib I have been using for quite a while.

[http://mirven.github.io/underscore.lua/](http://mirven.github.io/underscore.lua/)

~~~
Spiritus
Looks cool, but hasn't been updated in four years.

~~~
draegtun
_mirven_ does seem to have gone quiet (on Github & Twitter).

Two more up-to-date forks can be found here....

\- [https://github.com/jtarchie/underscore-
lua](https://github.com/jtarchie/underscore-lua)

\- [https://github.com/mark-otaris/underscore.lua](https://github.com/mark-
otaris/underscore.lua)

NB. The _jtarchie_ fork seems best because its got 59 stars & 5 contributors.

------
jbeja
I wish that Lua get more attention.

~~~
stevekemp
Agreed. Since I started using Lua as the scripting language for my (console)
mail client I've had a lot of fun with it.

------
limmeau
Apparently, this is part of an in-memory NoSQL database with Lua stored
procedures, called Tarantool. Has anyone used that?

[http://www.tarantool.org/](http://www.tarantool.org/)

------
eliteraspberrie
Is this similar to the functools package in Python? If so, that would
certainly convince me to use Lua for some of what I use Python for.

~~~
rtsisyk
Yeah, the library has some common functions with "itertools" Python's module
[1]. fun.range syntax was even copied from Python's range. What is more, you
can also take a look on Haskell's Data.List [2] or StandardML's List [3]
packages.

[1]
[http://docs.python.org/2/library/itertools.html](http://docs.python.org/2/library/itertools.html)
[2] [http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-
Li...](http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-List.html)
[3]
[http://www.standardml.org/Basis/list.html](http://www.standardml.org/Basis/list.html)

------
islon
I was just looking for that! Can it be used with the normal lua interpreter
too? I want to use it on my löve game, so, no luaJIT.

------
ufo
> Reference Lua may not work and is not supported

Any particular reason for that?

~~~
rtsisyk
The initial idea was to make a functional library that works best with tracing
JIT compiler. Functional paradigm on tracing JIT is the core innovation in the
project.

Lua 5.1 can run fun.lua after minor changes (I'll make patch soon), but I am
not sure that performance will be good enough.

~~~
ufo
Do you think the performance is really going to be that bad in the regular Lua
interpreter? I wonder if its going to be better or worse than the
underscore.lua approach of using arrays instead of iterators.

Also, are you using lots of 5.1 specific stuff (setfenv, ...) or would this
also be easily ported to 5.2?

~~~
rtsisyk
The library code is Lua 5.1 compatible (except bitwise functions in "operator"
submodule, but it can be easily replaced with "bit32" or even removed).

Some high-order functions heavily use recursion just to support multireturn
iterators (yes, you can use multireturn with map, reduce, filter and so on). I
plan to benchmark these parts and add optimized versions for non-JIT Lua if
necessary.

I will try to make a patch in the next couple of days. Thanks for your
interest!

------
Goranek
it would be nice to have this or underscore library inside redis

~~~
aktau
according to antirez, redis does not (and will not) support LuaJIT. Even
though at one point there was someone who had got it working. From experience
I know that replacing Lua with LuaJIT, even in a statically compiled project,
is quite easy. The reasoning is of course that lua code for redis shouldn't
have to do a lot of processing, and thus won't benefit from the extra
performance (it also shouldn't run long enough for the JIT to come into
action).

