Mathematics is a symptom…

of the structuredness of the universe.

Spelled out a little, the argument runs like this: a completely chaotic universe not only would not be amenable to mathematical description, but would not be such that any kind of mathematics could be practised in it, since the practice of mathematics itself requires that it be possible for certain kinds of stable entities and entailments (a stock of symbols, arrangements of those symbols according to a syntax, repeatable procedures of induction and verification) to exist.

The existence of mathematics does not prove that the universe is in essence mathematical, but it does prove that the universe is capable of at least local stability and regularity, of which the stability and regularity of mathematics itself is a demonstrable instance. Phenomena that are amenable to mathematical description are by that token also instances of at least local stability and regularity.

There is no good reason to suppose that the universe is fundamentally chaotic, any more than we are obliged to suppose that the universe is fundamentally stable and regular. Chaos is not necessarily degenerate order; order is not necessarily arrested (or misperceived) chaos.

Enriching is Easy

Today I’d like to talk about a pattern that as far as I know is unique to Java 8. It makes use of the unusual way Java 8 implements lambdas as single-method interfaces, and the support for default methods in interfaces, to produce what I call “enriched lambdas”.

Here’s a simple example: a “Logged function” that logs every call that is made to it:

Now we can add the logging behaviour to any function by assigning or casting it to LoggedFunction rather than Function:

How does this work? LoggedFunction has only one method without a default implementation, so any lambda that matches the signature of this method can be cast to an instance of LoggedFunction. Instead of overriding the “apply” method of LoggedFunction, the lambda will override the “applyLogged” method, which the default “apply” method implementation delegates to.

Octarine uses this pattern in a variety of ways, notably to build up Schemas, Serialisers and Deserialisers from single lambda expressions:

In this case, the unimplemented method is used to configure the object, and all other methods (with their default implementations) use this method to obtain their configuration.

This pattern is similar in some respects to the “enrich my library” pattern in Scala, which uses implicits – it provides a safe and convenient way to “upcast” simple lambdas to objects with enriched behaviour.

%d bloggers like this: