Identity

The identity property of some operation states that if one of the operands is the identity value, then the result of the operation will be equal to another operand. For multiplication, the identity value is one; for addition, it is zero. Because of the commutativity of both multiplication and addition, the identity value can appear in any position. For example, in the next snippet the identity element appears as the first and as the second operand for all of them:

scala> forAll((a: Int) => a + 0 == a && 0 + a == a).check
+ OK, passed 100 tests.
scala> forAll((a: Int) => a * 1 == a && 1 * a == a).check
+ OK, passed 100 tests.
scala> forAll((a: String) => a + "" == a && "" + a == a).check
+ OK, passed 100 tests.

For string concatenation, the identity is an empty string. It turns out our conditional commutativity property for strings was just a manifestation of the universal identity property!

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

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