Module: EmanLib

Defined in:
lib/emanlib.rb,
lib/patch/lambda.rb

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.support_lambdaObject

Support for using a _ as the second operand with operators. WARN: This method WILL MODIFY the standard library classes. In particular, the operators: ‘- * / % ** & | ^ << >> <=> == === != > < >= <=` in the classes: `Integer, Float, Rational, Complex, Array, String, Hash, Range, Set`



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/patch/lambda.rb', line 167

def support_lambda
  [[Integer, Float, Rational, Complex, Array, String, Hash, Range, Set],
   i[- * / % ** & | ^ << >> <=> == === != > < >= <=]].op(:product)
    .each do |klass, op|
    next unless klass.instance_methods(false).include?(op)

    original = klass.instance_method(op)
    klass.define_method(op) do |other|
      if other.is_a?(Lambda)
        repr = [self]
        repr.concat(other.repr)
        repr << Lambda::Function.new(op)
        Lambda.new(repr)
      else
        original.bind(self).call(other)
      end
    end
  end
end