class Proc

Public Class Methods

wrap(arg) click to toggle source

Wrap the argument in a proc. Simply returns the argument if it is already a Proc object.

TODO: Would this make more sense as `Kernel#Proc()` method,

akin to %x`Integer()` and the like?

Returns [Proc]

Credit: Sean Mackesey

# File lib/core/facets/proc/wrap.rb, line 13
def self.wrap(arg)
  Proc === arg ? arg : Proc.new{ arg }
end

Public Instance Methods

*(x) click to toggle source

Operator for #compose and Integer#times_collect/of.

a = lambda { |x| x + 4 }
b = lambda { |y| y / 2 }

(a * b).call(4)  #=> 6
(b * a).call(4)  #=> 4

CREDIT: Dave

# File lib/core/facets/proc/compose.rb, line 29
def *(x)
  if Integer===x
    # collect times
    c = []
    x.times{|i| c << call(i)}
    c
  else
    # compose procs
    lambda{|*a| self[x[*a]]}
  end
end
bind(object) click to toggle source

Bind a Proc to an object returning a Method.

NOTE: This version comes from Rails. The old Facets version used thread.rb, but I no longer think the implementaiton is thread critical. Please make a bug report if this proves wrong.

# File lib/core/facets/proc/bind.rb, line 12
def bind(object)
  block, time = self, Time.now
  method_name = "__bind_#{time.to_i}_#{time.usec}"
  object.singleton_class.class_eval do
    define_method(method_name, &block)
    method = instance_method(method_name)
    remove_method(method_name)
    method
  end.bind(object)
end
bind_to(object) click to toggle source

Bind a procedure to an object. This works by wrapping instance_eval on the Proc object and then wrapping this in a new Proc.

a = [1,2,3]
p1 = Proc.new{ join(' ') }
p2 = p1.bind_to(a)
p2.call  #=> '1 2 3'
# File lib/core/facets/proc/bind_to.rb, line 12
def bind_to(object)
  Proc.new{object.instance_eval(&self)}
end
compose(g) click to toggle source

Returns a new proc that is the functional composition of two procs, in order.

a = lambda { |x| x + 4 }
b = lambda { |y| y / 2 }

a.compose(b).call(4)  #=> 6
b.compose(a).call(4)  #=> 4

CREDIT: Dave

# File lib/core/facets/proc/compose.rb, line 14
def compose(g)
  raise ArgumentError, "arity count mismatch" unless arity == g.arity
  lambda{ |*a| self[ *g[*a] ] }
end
partial(*args) click to toggle source

Convert a Proc object into new partial Proc object.

a = proc { |a,b,c| a+b+c }
b = a.partial(NA,2,NA)
b[1,3] #=> 6

Note, the #__ method, which used to be used in stay of NA, has been deprecated.

This method is similar to Proc#curry.

CREDT Trans

# File lib/core/facets/proc/partial.rb, line 24
def partial(*args)
  Proc.new do |*spice|
    result = args.collect do |a|
      NA == a ? spice.pop : a
    end
    call(*result)
  end
end
to_method(object, name=nil) click to toggle source

Convert Proc to method.

object = Object.new

function = lambda { |x| x + 1 }

function.to_method(object, 'foo')

object.foo(1)  #=> 2
# File lib/core/facets/proc/to_method.rb, line 16
def to_method(object, name=nil)
  ##object = object || eval("self", self)
  block, time = self, Time.now
  method_name = name || "__bind_#{time.to_i}_#{time.usec}"
  begin
    object.singleton_class.class_eval do
      define_method(method_name, &block)
      method = instance_method(method_name)
      remove_method(method_name) unless name
      method
    end.bind(object)
  rescue TypeError
    object.class.class_eval do
      define_method(method_name, &block)
      method = instance_method(method_name)
      remove_method(method_name) unless name
      method
    end.bind(object)
  end
end