Class Proc
In: lib/core/facets/proc/bind.rb
lib/core/facets/proc/to_method.rb
lib/core/facets/proc/compose.rb
lib/core/facets/proc/curry.rb
lib/core/facets/proc/update.rb
Parent: Object

Methods

*   bind   compose   curry   to_method  

External Aliases

call -> update
  Use a Proc as an observable.

CREDIT: Tim Pease

Public Instance methods

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

[Source]

    # 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 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.

[Source]

    # 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

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

[Source]

    # 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 Proc object into new Proc object.

TODO: Utilize Ruby 1.9‘s curry method.

[Source]

    # 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

Convert Proc to method.

  plusproc = lambda { |x| x + 1 }
  plusproc.to_method(self, 'foo')
  X.new.foo(1)  #=> 2

[Source]

    # 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

[Validate]