Class: Proc

Inherits:
Object
  • Object
show all
Defined in:
lib/mdarray/proc_util.rb

Overview

Proc methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clipped(predicate, method) ⇒ Object


This class verifies a condition and if true, returns the evaluation of a function, otherwise returns NaN.




121
122
123
# File 'lib/mdarray/proc_util.rb', line 121

def self.clipped(predicate, method)
  lambda { |*args| predicate.call(*args)? method.call(*args) : Float::NAN }
end

.complement(f) ⇒ Object


Builds a function that returns true when ‘f’ returns false, and vice versa.




50
51
52
# File 'lib/mdarray/proc_util.rb', line 50

def self.complement f
  lambda {|*args| not f.call(*args) }
end

.compose(f, g) ⇒ Object





33
34
35
# File 'lib/mdarray/proc_util.rb', line 33

def self.compose(f, g)
  lambda { |*args| f[g[*args]] }
end

.conjoin(*predicates) ⇒ Object


Builds a function which returns true whenever every function in ‘predicates’ returns true.




68
69
70
71
72
73
74
75
76
77
# File 'lib/mdarray/proc_util.rb', line 68

def self.conjoin(*predicates)

  base = lambda {|*args| true }
  predicates.inject(base) do |built, pred|
    lambda do |*args|
      built.call(*args) && pred.call(*args)
    end
  end

end

.constant(val) ⇒ Object


Returns a constant value




129
130
131
# File 'lib/mdarray/proc_util.rb', line 129

def self.constant(val)
  lambda { |arg| return val }
end

.disjoin(*predicates) ⇒ Object


Builds a function which returns true whenever any function in ‘predicates’ returns true.




85
86
87
88
89
90
91
92
93
94
# File 'lib/mdarray/proc_util.rb', line 85

def self.disjoin(*predicates)

  base = lambda {|*args| false }
  predicates.inject(base) do |built, pred|
    lambda do |*args|
      built.call(*args) || pred.call(*args)
    end
  end

end

.everywhereObject


Always returns true




137
138
139
# File 'lib/mdarray/proc_util.rb', line 137

def self.everywhere
  lambda { |arg| return true}
end

.identityObject


The identity function




153
154
155
# File 'lib/mdarray/proc_util.rb', line 153

def self.identity
  lambda { |arg| return arg }
end

.nowhereObject


Always returns false




145
146
147
# File 'lib/mdarray/proc_util.rb', line 145

def self.nowhere
  lambda { |arg| return false}
end

Instance Method Details

#*(g) ⇒ Object





41
42
43
# File 'lib/mdarray/proc_util.rb', line 41

def *(g)
  Proc.compose(self, g)
end

#bind1st(val) ⇒ Object


This method binds the 1st argument of a binary function to a scalar value, effectively enabling a binary function to be called in a context intended for a unary function.




102
103
104
# File 'lib/mdarray/proc_util.rb', line 102

def bind1st(val)
  lambda { |arg| self.call(val, arg) }
end

#bind2nd(val) ⇒ Object


This method binds the 2nd argument of a binary function to a scalar value, effectively enabling a binary function to be called in a context intended for a unary function.




112
113
114
# File 'lib/mdarray/proc_util.rb', line 112

def bind2nd(val)
  lambda { |arg| self.call(arg,val) }
end

#negObject





58
59
60
# File 'lib/mdarray/proc_util.rb', line 58

def neg
  Proc.complement(self)
end