Last active
November 14, 2018 06:49
-
-
Save RichOrElse/12d056be5757ec7ce540708bbac2b584 to your computer and use it in GitHub Desktop.
Partial Application in Ruby
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
class Method | |
def by(*head) | |
to_proc.by(*head) | |
end | |
def with(*head, &blk) | |
to_proc.with(*head, &blk) | |
end | |
def as(such) | |
to_proc.as(such) | |
end | |
end |
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
class Proc | |
alias_method :*, | |
def by(*head) return self if head.none? | |
curry(head.size.next).(*head) | |
end | |
alias_method :+, | |
def with(*tail, &block) return self & block if tail.none? | |
if arity == tail.size.next | |
proc { |head| call head, *tail, &block } | |
else | |
proc { |*head| call *head, *tail, &block } | |
end | |
end | |
def &(other) block = other.to_proc | |
if arity == 1 | |
proc { |head| call head, &block } | |
else | |
proc { |*head| call *head, &block } | |
end | |
end | |
alias_method :|, | |
def as(such) | |
such.to_proc & self | |
end | |
end |
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
class Symbol | |
def *(other) | |
other.public_method(self).to_proc | |
end | |
def by(head, *tail) | |
(self * head).by(*tail) | |
end | |
def with(*head, &blk) | |
to_proc.with(*head, &blk) | |
end | |
def as(such) | |
to_proc.as(such) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment