Module: ValueGraphTransformation::Arithmetic

Defined in:
lib/value_graph_transformation/arithmetic.rb

Overview

Basic arithmetic operations, like addition, subtraction, etc.

Defined Under Namespace

Classes: Add, ArithmeticFunctionFactory, Div, Mul, Sub

Class Method Summary (collapse)

Class Method Details

+ (Context) apply(context = Context.new, &arithmetic)

Apply the block with arithmetic operations to the given context

Parameters:

  • context (Context) (defaults to: Context.new)

    the context

  • arithmetic (Proc)

    the block to be invoked in the context of a ArithmeticFunctionFactory.

Returns:

  • (Context)

    the context in which the arithmetic operations were applied.

Specifications:

It returns a Context

The block is executed within the scope of ArithmeticFunctionFactory.

Arithmetic.apply {
  add(mul(7, 'x'), sub('a', div('42', 'y')), sum(['z', 47, 'u']))
}
%3 node0 7 node3 × node0->node3 node1 x node1->node3 node2 node16 + node2->node16 node3->node2 node4 42 node7 ÷ node4->node7 node5 y node5->node7 node6 node10 - node6->node10 node7->node6 node8 a node8->node10 node9 node9->node16 node10->node9 node11 z node15 + node11->node15 node12 47 node12->node15 node13 u node13->node15 node14 node15->node14 node16->node14

It applies the block operations to the given context

context = Arithmetic.apply {
  add(1337, 42, 'z')
}

Arithmetic.apply(context) {
  mul('x', 'y', 'z')
}
%3 node0 1337 node3 + node0->node3 node1 42 node1->node3 node2 z node3->node2 node4 x node6 × node4->node6 node5 y node5->node6 node6->node2


12
13
14
15
# File 'lib/value_graph_transformation/arithmetic.rb', line 12

def self.apply(context=Context.new, &arithmetic)
  ArithmeticFunctionFactory.new(context).instance_eval &arithmetic
  context
end