Created
November 27, 2015 23:06
-
-
Save domgetter/d6cd146bdea959ad3105 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# | |
# Please don't use this in any Ruby library or code. | |
# This is for proof-of-concept purposes only and will break Ruby functionality | |
# | |
class Proc | |
alias :greedy_curry :curry | |
def curry | |
if self.has_kwarg_parameters? | |
-> **kwargs { | |
if kwargs.length == kwarg_parameters.length | |
self[**kwargs] | |
else | |
-> **more_kwargs { | |
self[kwargs.merge more_kwargs] | |
} | |
end | |
} | |
else | |
greedy_curry | |
end | |
end | |
def kwarg_parameters | |
self.parameters.select {|(p,_)| p == :keyreq || p == :key || p == :keyrest} | |
end | |
def has_kwarg_parameters? | |
self.parameters.find {|(p,_)| p == :keyreq || p == :key || p == :keyrest} | |
end | |
end | |
print_name = -> first:,last: {puts "#{last}, #{first}"} | |
print_name.curry[first: "Jim", last: "Smith"] | |
#=> Smith, Jim | |
print_name.curry[last: "Smith", first: "Jim"] | |
#=> Smith, Jim | |
print_name.curry[first: "Jim"][last: "Smith"] | |
#=> Smith, Jim | |
print_name.curry[last: "Smith"][first: "Jim"] | |
#=> Smith, Jim | |
# This version only works two calls deep, but with recursion could be made more robust. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment