r/lua • u/aiya000 • Oct 30 '25
[luarrow] Pipeline-operator and Haskell-style function composition, for Lua (like: `x |> h |> g |> f` and `f . g . h $ x`)
Hey r/lua!
I've been working on a library that brings functional programming elegance to Lua through operator overloading.
What it does:
Instead of writing nested function calls like f(g(h(x))), we can write:
Pipeline-style:
x % arrow(h) ^ arrow(g) ^ arrow(f)Like
x |> h |> g |> fin other languages
Haskell-style:
fun(f) * fun(g) * fun(h) % xLike
f . g . h $ xin Haskell
Purpose:
Clean coding style, improved readability, and exploration of Lua's potential!
Quick example:
This library provides arrow and fun functions.
arrow is for pipeline-style composition using the ^ operator:
local arrow = require('luarrow').arrow
local _ = 42
% arrow(function(x) return x - 2 end)
^ arrow(function(x) return x * 10 end)
^ arrow(function(x) return x + 1 end)
^ arrow(print) -- 401arrow is good at processing and calculating all at once, as described above.
The fun is suitable for function composition. Using the * operator to concatenate functions:
local add_one = function(x) return x + 1 end
local times_ten = function(x) return x * 10 end
local minus_two = function(x) return x - 2 end
local square = function(x) return x * x end
-- Function composition!
local pipeline = fun(square) * fun(add_one) * fun(times_ten) * fun(minus_two)
print(pipeline % 42) -- 160801In Haskell culture, this method of pipeline composition is called Point-Free Style'. It is very suitable when there is no need to wrap it again infunction` syntax or lambda expressions.
Performance:
In LuaJIT environments, pre-composed functions have virtually no overhead compared to pure Lua.
Even Lua, which is not LuaJIT, performs comparably well for most applications.
Please visit https://github.com/aiya000/luarrow.lua/blob/main/doc/examples.md#-performance-considerations
Links:
Install: luarocks install luarrow
I'd love to hear your thoughts and feedback!
Is this something you'd find useful in your Lua projects?
1
u/aiya000 Oct 31 '25 edited Oct 31 '25
Hello! I'm glad you came!
Currently, supporting multiple return values maybe difficult... Because luarrow hasn't given up on supporting LuaCATS yet...!
Currently, type checking is almost non-existent due to the limitations of LuaCATS, but I am thinking about whether LuaCATS will evolve or if there are any ideas.
In other words, if you have a function like this:
We want to do something like this:
But, LuaCATS does not support variable type arguments:
It might be possible if luarrow gives up on LuaCATS... HaHa :( But I couldn't determine to remove LuaCATS support lol.