Class: Prelude::Lambda

Inherits:
Proc
  • Object
show all
Defined in:
lib/prelude/lambda.rb

Overview

$Id: lambda.rb 34 2007-10-23 21:38:09Z prelude $

Somewhat extended version of Ruby’s native lambda/Proc that defines all needed methods. We probably can get away with modifying the Proc class, but this approach seems safer.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Lambda

Returns a new instance of Lambda.



32
33
34
# File 'lib/prelude/lambda.rb', line 32

def initialize(&block)
  Proc.new(&block)
end

Instance Method Details

#*(right) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/prelude/lambda.rb', line 43

def *(right)
  Lambda.new {|*a| 
    #p "* #{self.inspect} #{right.inspect} #{a.inspect}"

#         req = right.arity
#         if req > 0
#           right_args = a[0..req-1]
#           left_args = a[req..-1]
#         else
#           right_args = a
#           left_args = []
#         end

#         p "right_args #{right_args.inspect}"
#         right_res = right[*right_args]

#         p "right_res #{right_res.inspect}"
#         if req > 0
#           left_args = [right_res] + left_args
#         else
#           left_args = right_res
#         end
#         p "left_args #{left_args.inspect}"
#         self[*left_args]

    # Compact version of the above (delete the above when done!)
    req = right.arity
    if req > 0
      self[*([right[*a[0..req-1]]] + a[req..-1]) ]
    else
      self[*right[*a]]
    end
    
  }
end

#call(*args) ⇒ Object Also known as: []



37
38
39
40
# File 'lib/prelude/lambda.rb', line 37

def call(*args)
  #p "call #{self.inspect} #{args.inspect} arity #{self.arity}"
  proc_call(*args)
end

#curry(one) ⇒ Object



79
80
81
# File 'lib/prelude/lambda.rb', line 79

def curry(one)
  Lambda.new { |*args| self[one, *args] }
end

#proc_callObject



36
# File 'lib/prelude/lambda.rb', line 36

alias proc_call call

#uncurryObject



83
84
85
# File 'lib/prelude/lambda.rb', line 83

def uncurry()
  Lambda.new { |*args| self[args[0]] }
end