Skip to content

Instantly share code, notes, and snippets.

@RichOrElse
Last active November 14, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichOrElse/12d056be5757ec7ce540708bbac2b584 to your computer and use it in GitHub Desktop.
Save RichOrElse/12d056be5757ec7ce540708bbac2b584 to your computer and use it in GitHub Desktop.
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