A mixin with which to extend a singleton class that curries arguments on a function defined by `#def_function`.
module Curryable | |
def def_function &block | |
@function = block | |
end | |
def call arg | |
unless @function | |
raise StandardError, 'must define a function with def_function' | |
end | |
function_chain.(arg) | |
end | |
private | |
def function_chain *previous_args | |
lambda do |arg| | |
args = previous_args.dup << arg | |
if terminate_chain?(args.size) | |
@function.(*args) | |
else | |
function_chain(*args) | |
end | |
end | |
end | |
def terminate_chain? args_size | |
(arity - args_size) == 0 | |
end | |
def arity | |
@function.arity | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment