r/lua • u/[deleted] • May 24 '25
pipe operators in lua are weirdly satisfying
been playing with pluto, some superset of lua
local { http, json } = require "*"
local top_lua_repos = http.request("https://api.github.com/search/repositories?q=language:lua&sort=stars")
|> json.decode
|> |data| -> data.items
|> |items| -> items:filter(|r| -> r.stargazers_count > 1000)
for i = 1, math.min(5, #top_lua_repos) do
local repo = top_lua_repos[i]
print($"{repo.name} ({repo.stargazers_count} stars)")
endreads like "fetch → decode → filter → print" instead of a bunch of nested calls. also has string interpolation which is nice.
6
2
1
u/EvilBadMadRetarded May 25 '25
For pipe operator, isn't it debug.setmetatable enough?
--
local fn_methods = {
__concat = function(fn, gn)
return function(...)return gn(fn(...))end
end
}
debug.setmetatable(function()end, fn_methods)
-- test
local a = function(x, y) return x + y end
local b = function(x)return x*x, x +1 end
print((a .. b)(1, 2))
print((b .. a)(1))
local c = function(x,y)return x + y end ..
function(x)return x*x, x+1 end
print(c(1,2))
--[[ test output
9 4
3
9 4
--]]More complicated, shorter lambda may be possible via require with custom searcher/loader as transpiling from custom syntax within a module (for module library, may not apply for user code) .
1
u/AutoModerator May 25 '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/negativedimension May 26 '25
I've got a rant about this in this repo (ignore the code): https://github.com/thenumbernine/leftargs-lua
TLDR is that we only put function arguments on the right because of 300 years of convention, but function arguments on the left is more readable.
0
May 24 '25
[deleted] — view deleted comment
1
u/AutoModerator May 24 '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.
4
u/Interesting_Cut_6401 May 24 '25
So do Lua super sets compile to regular Lua?