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) -- 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:
- 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?
4
u/[deleted] Oct 30 '25
Look into debug.setmetatable, I think you will find it useful
2
u/EvilBadMadRetarded Oct 31 '25
Hello, again :) Yet, not support multiple return values? .
2
u/jisifu Oct 31 '25
Yuescript looked promising. But if this is a smaller library, then maybe lua can be a little more composeable
2
u/RedNifre Oct 31 '25
Very nice! I think you could do the pipeline composition without the extra "arrow" wrapper, i.e. the ^ operator could check if the second parameter is a not-arrowed function and arrow it if necessary.
1
u/appgurueu Nov 03 '25
If I want to abstract function composition in Lua, I'd write something like
and that's it. If I want to make that a bit neater, I might make it variadic (this I might put into a utility library):
Then I can write your example as:
and that's it. Much more readable, much more flexible, much more simple (only functions are involved; no abuse of arithmetic metamethods and custom objects). By not abusing operators, this can also support variadic functions.
Though really: I don't think this is a good choice of example at all. Because you could, and should, just write
square(add_one(times_ten(minus_two(42)))). If that's not readable, introduce some significant variables.composedoesn't really help here. But really, this is just a simple arithmetic expression, so you would just write(((42 - 2) * 10) + 1)^2.I've replied at greater length on r/functionalprogramming: https://www.reddit.com/r/functionalprogramming/comments/1omejzk/comment/nmta8zo/
2
u/AutoModerator Oct 30 '25
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/[deleted] Oct 30 '25 edited Oct 30 '25
[removed] — view removed comment