Actions
Feature #15302
openProc#with and Proc#by, for partial function application and currying
Status:
Open
Assignee:
-
Target version:
-
Description
Proc#by allows currying implicitly
class Proc
def by(*head)
return self if head.none?
curry(head.size.next).(*head)
end
end
class Method
def by(*head)
to_proc.by(*head)
end
end
class Symbol
def by(*head)
to_proc.by(*head)
end
end
double = :*.by(2) # => proc { |n| 2 * n }
Proc#with pre-defines trailing arguments and/or block.
class Proc
def with(*tail, &blk)
if arity == tail.size.next
proc { |head| call head, *tail, &blk }
else
proc { |*head| call *head, *tail, &blk }
end
end
end
class Method
def with(*head, &blk)
to_proc.with(*head, &blk)
end
end
class Symbol
def with(*head, &blk)
to_proc.with(*head, &blk)
end
end
double = :*.with(2) # => proc { |n| n * 2 }
That's the basic idea, but I've also expanded on it by optimising and defining operators (+, &, |) and other methods (Proc#such) here.
Actions
Like0
Like0Like0Like0Like0