Skip to content

Instantly share code, notes, and snippets.

@RichOrElse RichOrElse/method.rb
Last active Nov 14, 2018

Embed
What would you like to do?
Partial Application in Ruby
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
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
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
You can’t perform that action at this time.