Associativity

The associativity is the same for operators as commutativity is for operands—if there are multiple operations, then the order in which operations are performed does not matter as long as the order of operands does not change.

The associativity properties for multiplication and addition again look very similar, as in the following example where we have three properties, each comparing results of two computations with a different order of operations:

scala> forAll((a: Int, b: Int, c: Int) => (a + b) + c == a + (b + c)).check
+ OK, passed 100 tests.
scala> forAll((a: Int, b: Int, c: Int) => (a * b) * c == a * (b * c)).check
+ OK, passed 100 tests.
scala> forAll((a: String, b: String, c: String) =>
(a + b) + c == a + (b + c)).check
+ OK, passed 100 tests.

The last line demonstrates that string concatenation is associative as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.88