Whither Extension Methods?
Apparently, there’s a java dialect called XTend which does this, but there’s no obvious equivalent of extension methods in Java.
An extension method, as I recall from C#, allows you to define something like this:
You can do the second of these in Java, and why wouldn’t you!?
Reusability in Streams
In a Java stream, we use a lovely fluent syntax. Let’s consider the following example:
The above algorithm is in three phases:
- Select the source of data
- Drill down into the
- Convert to non-blank string rendered in upper case
The latter two of these might be turned into reusable components:
The problem with this is that using them looks like it’s going to involve some nesting:
Not only is that a bit hard to read, but it’s backwards, compared with the nice fluent/forwards nature of the original.
We Can Solve This Via Streams Though!
It occurred to me today, though, that this can be solved via streams in Java. It’s definitely worth considering other ways that streams and Optional’s stream-like interface can make us compose transformations fluently:
This is nearly in the category of useful…
Extensions would be better, though…