A binary operation combines two values. The especially useful form takes two values of one type and returns a new value of that same type: a closed operation. In Java, that shape is `BinaryOperator<T>`, a specialization of `BiFunction<T,T,T>`. Integer addition, string concatenation, Boolean `||`, and `Stream.concat` all fit the pattern. section 7.2.1
Listing 7.2 Java’s BinaryOperator interface
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {} #A`BinaryOperator<T>` makes the same-input, same-output shape explicit in the type system. section 7.2.1
The useful design move is not merely writing a two-argument function. It is noticing the operation already hidden inside stateful or object-oriented code. A method such as `BigDecimal.ONE.add(BigDecimal.ONE)` is still combining two values, even though one operand is the receiver. Seeing that shape shifts attention from mutation and control flow toward what values are being combined. section 7.2.1 section 7.3
Grouping duplicate records
A conventional grouping loop seeds a map with an empty list, retrieves a list, and mutates it with `add`. Reframe each item as a one-entry map, then combine maps. When keys collide, combine their list values with a list operation that accepts two lists and returns a new list. The resulting `groupBy` can be expressed as a stream `map` followed by `reduce`. This is not automatically more efficient, but it exposes the combining rule as an independently named and reusable operation. section 7.3 section 7.3
When assessing a binary operation, make its behavior under repeated use explicit:
- Check argument order. `x - y` and `y - x` show that reversing inputs can change the result. section 7.4
- Check grouping. `add(add(a, b), c)` and `add(a, add(b, c))` may differ, which matters for reductions. section 7.4
- Check the actual data type rather than assuming familiar laws. Floating-point addition can produce different answers under different groupings. section 7.4
These behaviors are algebraic properties: relationships among inputs, outputs, orderings, and groupings that say what correctness means for an operation. Writing claims with “for all” notation helps identify the relevant states and invariants instead of leaving business meanings implicit. For conflicting records, an initial claim might state that combining any two records produces a more favorable record; that claim then needs refinement into testable properties. section 7.4 section 7.5.2
