Pondering on the right lambda for the job
Pondering on the right lambda for the job

With Java 8 comes a whole new set of language features. It even challenges the imperative coding-style of the Java programmer.

Java is a core language in Finn.no. More and more of our java-modules are being built with Java 8, adopting new features of the language. A workshop was warranted, with the goal of bringing every developer up to speed on the functional paradigm of Java 8.

In Finn.no, we want programmers to do stuff like this

db.fetchLastDayAds()
  .stream()
  .filter(ad -> "bap-webstore".equals(ad.getAdType()))
  .flatMap(ad -> ad.getContacts().stream())
  .distinct()
  .flatMap(per ->
    Optional.ofNullable(contact.getEmail()).map(Stream::of)
    .orElseGet(Stream::empty))
  .peek(contact -> LOG.trace("Sending notification to "+per))
  .forEach(this::sendNotification);

While avoiding stuff like this

IntStream.iterate(0, i -> (i + 1) % 2)
         .parallel()
         .distinct()
         .limit(10)
         .forEach(System.out::println);

(Locking up all cores on a CPU is bad, and should only be done when the machine is right about to become self-aware and turn against you.)

Several lambda-arrows pointing in the right direction here
Several lambda-arrows pointing in the right direction here

We split the workshop into two half days. The first day was dedicated to streams and lambdas. Everyone seemed keen on getting those tests green (a rhyme!).

Day 2 we raised the bar with Optional and our in-house version of Either<L,R>. With these structures, much more code can be written functionally in a world where values might not exist (be null), and things may go wrong (throw exceptions).

We need power.. lots of power!
We need power.. lots of power!

Both the word “monad” and the phrase “monadic domain” was uttered several times, but we still saw very few making the swoooosh-sound while flying a hand over their head (the internationally recognized sign of communicating that a topic is beyond mental capacity).

This might mean that the timing was good, and developers are interested in the new features of Java 8.

You may checkout the project and do the tasks yourself, by making the failing tests green.

Tags: java java8 functional lambda workshop