Last active
January 2, 2016 06:19
-
-
Save davejachimiak/8262343 to your computer and use it in GitHub Desktop.
A mixin with which to extend a singleton class that curries arguments on a function defined by `#def_function`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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