Skip to content

Instantly share code, notes, and snippets.

@rubenwardy rubenwardy/pipeline.lua
Last active Jul 6, 2018

Embed
What would you like to do?
-- Run a series of functions as a pipeline - each returning the next's arguments.
--
-- The first function will be ran immediately
--
-- @param interval Time between pipelines
-- @param funcs A list of functions, each one of the form function(...)
-- where ... is returned by the previous function.
-- @param params Optional, a list of arguments to pass to the first function
local function pipeline(interval, funcs, params)
if funcs[1] ~= nil then
params = table.pack(funcs[1](table.unpack(params)))
end
table.remove(funcs, 1)
if #funcs > 0 then
minetest.after(interval, pipeline, interval, funcs, params)
end
end
-- Run a series of functions as a pipeline - each returning the next's arguments.
-- Player is injected as the first argument in functions.
--
-- The first function will be ran immediately
--
-- @param name Player name
-- @param interval Time between pipelines
-- @param funcs A list of functions, each one of the form function(player, ...)
-- where ... is returned by the previous function.
-- @param params Optional, a list of arguments to pass to the first function
local function pipeline_plr(name, interval, funcs, params)
if funcs[1] ~= nil then
local player = minetest.get_player_by_name(name)
if not player then
return
end
table.insert(params, 1, player)
params = table.pack(funcs[1](table.unpack(params)))
end
table.remove(funcs, 1)
if #funcs > 0 then
minetest.after(interval, pipeline_plr, name, interval, funcs, params)
end
end
pipeline(1, {
function()
return 5.3
end,
function(x)
return 2*x
end,
math.round,
nil, -- skip a step, ie: 2xInterval is between above and below functions
function(x)
return x*x, x*x*x
end,
function(x, y)
return "X is: " .. x .. ", y is: " .. y
end,
print
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.