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) % x- Like
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) -- 401
arrow 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) -- 160801
In 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:
- GitHub: https://github.com/aiya000/luarrow.lua
- Install: luarocks install luarrow
I'd love to hear your thoughts and feedback!
Is this something you'd find useful in your Lua projects?
View all comments
Show parent comment
2
u/[deleted] Oct 30 '25
I like this syntax better, but yeah I think it should be ok because we have LuaLS annotations to tell us it won't work. Also, debug.setmetatable lets you set the meta table of every function, so you can do stuff like
local function f(x) return 2*x end local function g(x) return x2 end
local F = f ^ g
Etc