Parent

Namespace

Proc

Public Instance Methods

*(x) click to toggle source

Operator for Proc#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
29:   def *(x)
30:     if Integer===x
31:       # collect times
32:       c = []
33:       x.times{|i| c << call(i)}
34:       c
35:     else
36:       # compose procs
37:       lambda{|*a| self[x[*a]]}
38:     end
39:   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 10
10:   def bind(object)
11:     block, time = self, Time.now
12:     (class << object; self; end).class_eval do
13:       method_name = "__bind_#{time.to_i}_#{time.usec}"
14:       define_method(method_name, &block)
15:       method = instance_method(method_name)
16:       remove_method(method_name)
17:       method
18:     end.bind(object)
19:   end
bind_to(object) click to toggle source
   # File lib/core/facets/proc/bind_to.rb, line 4
4:   def bind_to(object)
5:     Proc.new{object.instance_eval(&self)}
6:   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
14:   def compose(g)
15:     raise ArgumentError, "arity count mismatch" unless arity == g.arity
16:     lambda{ |*a| self[ *g[*a] ] }
17:   end
curry(*args) click to toggle source

Curry Proc object into new Proc object.

TODO: Utilize Ruby 1.9’s # method.

    # File lib/core/facets/proc/curry.rb, line 7
 7:   def curry(*args)
 8:     if args.empty?
 9:       idx = (0...arity).to_a
10:     else
11:       raise ArgumentError, "argument count is greater than prok.arity (#{args.size} > #{arity})" if args.size > arity
12:       raise ArgumentError, "arguments must be unique indexes" if args.uniq != args
13:       raise ArgumentError, "arguments must be indexes" if args.any?{ |a| !Fixnum===a }
14:       idx = (0...arity).to_a
15:       idx = args + (idx - args)
16:     end
17: 
18:     pro = self
19:     rec = ''
20:     idx.each do |i|
21:       rec << "proc { |a#{i}| "
22:     end
23:     rec << "pro["
24:     rec << (0...arity).to_a.collect{|i| "a#{i}"}.join(',')
25:     rec << "]"
26:     rec << "}" * arity
27: 
28:     instance_eval rec
29:   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(X,2,X)
  b[1,3] #=> 6

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

This method is similar to Proc#curry.

CREDT Trans

    # File lib/more/facets/partial.rb, line 17
17:   def partial(*args)
18:     Proc.new do |*spice|
19:       result = args.collect do |a|
20:         X == a ? spice.pop : a
21:       end
22:       call(*result)
23:     end
24:   end
to_method(object, name=nil) click to toggle source

Convert Proc to method.

  plusproc = lambda { |x| x + 1 }
  plusproc.to_method(self, 'foo')
  X.new.foo(1)  #=> 2
    # File lib/core/facets/proc/to_method.rb, line 11
11:   def to_method(object, name=nil)
12:     #object = object || eval("self", self)
13:     block, time = self, Time.now
14:     method_name = name || "__bind_#{time.to_i}_#{time.usec}"
15:     begin
16:       (class << object; self; end).class_eval do
17:         define_method(method_name, &block)
18:         method = instance_method(method_name)
19:         remove_method(method_name) unless name
20:         method
21:       end.bind(object)
22:     rescue TypeError
23:       object.class.class_eval do
24:         define_method(method_name, &block)
25:         method = instance_method(method_name)
26:         remove_method(method_name) unless name
27:         method
28:       end.bind(object)
29:     end
30:   end
to_openobject() click to toggle source

Translates a Proc into an OpenObject. By droping an OpenObject into the Proc, the resulting assignments incured as the procedure is evaluated produce the OpenObject. This technique is simlar to that of MethodProbe.

  p = lambda { |x|
    x.word = "Hello"
  }
  o = p.to_openobject
  o.word #=> "Hello"

NOTE The Proc must have an arity of one —no more and no less.

     # File lib/more/facets/openobject.rb, line 260
260:   def to_openobject
261:     raise ArgumentError, 'bad arity for converting Proc to openobject' if arity != 1
262:     o = OpenObject.new
263:     self.call( o )
264:     o
265:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.