liveBook · Manning

Binary operation

As explained in Data-Oriented Programming in Java

A binary operation is a rule that takes exactly two operands from a specified set or type and produces a result.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Defines closed operations, works through list and map refactorings, then introduces ordering and grouping properties without fully specifying them.
Examples
Uses addition, concatenation, Boolean operators, stream concatenation, duplicate-record grouping, list combination, map merging, subtraction, and floating-point addition.
What it assumes
Assumes intermediate Java familiarity with generics, collections, lambdas, streams, and object methods while introducing algebraic vocabulary directly.
What you can do after
Evaluate/understand Java combination code by identifying closed operations and checking whether order and grouping affect results.
How it connects
Connects BinaryOperator, BiFunction, Stream.reduce, map-reduce, algebraic properties, invariants, and formal notation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.