Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(func) Extend compose to suport N functions #448

Merged
merged 4 commits into from
Dec 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lua/pl/func.lua
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,28 @@ utils.add_function_factory(_PEMT,func.I)
func.bind1 = utils.bind1
func.curry = func.bind1

--- create a function which chains two functions.
--- create a function which chains multiple functions.
-- a table of multiple functions can also be
-- passed instead of providing functions as
-- individual parameters.
-- @func f a function of at least one argument
-- @func g a function of at least one argument
-- @param ... additional functions to compose
-- @return a function
-- @usage printf = compose(io.write,string.format)
function func.compose (f,g)
return function(...) return f(g(...)) end
-- @usage printf = compose(io.write, string.format)
-- @usage printf = compose({ io.write, string.format })
-- @usage printf = compose(io.write, string.lower, string.format)
-- @usage printf = compose({ io.write, string.lower, string.format })
function func.compose (...)
local args = pack(...)
if type(args[1]) == "table" then
args = args[1]
end
broma0 marked this conversation as resolved.
Show resolved Hide resolved
return tablex.reduce(function(f, g)
return function(...)
return f(g(...))
end
end, args)
end

--- bind the arguments of a function to given values.
Expand Down