This blog post is extracted from a talk I gave at St. Petersburg’s excellent Joker Conf in 2018.
The goal of this post is to examine whether the cost-benefit trade-off stacks up for functional programming in Java across 3 dimensions
Correctness
Performance
& Complexity
An informal definition of correctness is :
an algorithm is correct if it is error free.
In Java we could define hierarchy of errors as something like
Compile time errors : errors the Java compiler catches and alerts us about.
Runtime errors : Runtime exceptions and errors the Java runtime (hopefully) alerts us about.
Logic errors…
A roundup of the last 2 weeks blogs and talks (with the winding down of conference season the throughput of blogs and talks has fallen slightly).
Full Stack Reactive Java with Project Reactor and Spring Boot by Mark Heckler
Reactive Java a new asynchronous database access API by Kuassi Mensah
From Functional to imperative programming by Francisco Alvarez
Functional Java by Example | Part 6 — Functions as Parameters by Ted Vinke
The Functional Style — Part 7 by Richard Wild
Spring, Kotlin and the Functional Way by Sébastien Deleuze
Java Functional Retry with resilience4j-retry by mahmoud romeh
Tagless final is all the rage in the Scala community at the moment. It is a technique that allows you to define the general structure of the code to be executed, such that we can configure different implementations (or interpreters) to inject effects such as Optionality, Asynchronicity, Parallelism, Non-Determinism, Error Handling depending on our needs. Need to simplify a highly concurrent piece of code for a test case so that you can debug a nasty production issue? With Tagless final — no problem!
Much like the Free Monad, Tagless Final allows us to define mini-DSLs and apply different interpreters to…
A bumper round up of blogs and talks on Functional Programming & Reactive Programming in Java.
Lambdas are not functional programming by John McClean
OOP v̶s̶ and FP by Mario Fusco
Functional architecture: a definition by Mark Seemann
FP vs OOP: Choose Two by Brian Goetz
Is Functional Programming worth it? by John McClean
Functional Programming Patterns With Java 8 by Victor Rentea
Introduction to AWS SDK v2 for Java by Maciej Walkowiak
Reactive Java For the Realtime : video and slides by Trayan Iliev
Dysfunctional programming in Java 7 : Immutable Collections by john mcclean
No-one in Java-land is doing functional programming, and that is a good thing.
Just because you are using Lambda expressions, does not mean you are doing functional programming.
Lambda expressions in Java are simply a less verbose way of creating (slightly constrained) Objects and as such the most likely outcome of adopting Lambda’s without a good understanding of core functional concepts is gnarly, twisted, hard to follow, obfuscated imperative Object Oriented code with a nice concise syntax. Yes, we could write exactly the same dodgy code in a more verbose way by creating individual classes and lambdas.
Conversely, even when…
In episode 2 of Dysfunctional Programming in Java we covered why and how to make your Java Objects Immutable, in this article we will dive deeper with Immutable Collections. (Elsewhere in the series we have covered laziness, functional composition, null handling, error handling, and concurrency.)
Method signatures like this are painfully common in Java
A bumper round up of blogs and talks on Functional Programming & Reactive Programming in Java.
Exploring Reactive Programming with Java by Miro Cupak
Introduction to Reactive APIs with Postgres, R2DBC, Spring Data JDBC and Spring WebFlux by Piotr Mińkowski
Full-stack Reactive Kotlin with Project Reactor & Spring Boot 2 by Mark Heckler
Java Streams vs Reactive Streams: Which, When, How, and Why? by Venkat Subramaniam
Reactive Relational Database Connectivity by Ben Hale
Reactive Spring Security 5.1 by Example by Rob Winch (I hope he is not sad! 😀)
Its time to see how adopting principles from functional programming can help us simplify the challenge of implementing concurrent and parallel code in Java. So far we’ve explored how laziness, immutability, functional composition, banning null assignments and throwing exceptions can bring calmness and serenity to our code base. Concurrency produces the most hair-pullingly-frustrating bugs of all.
Let’s start with some really dysfunctional imperative code
Given a number of non-volatile, non-final fields
The Functional Style — Part 6 by Richard Wild
Functional Programming (not necessarily with Java!) by Alex Macavei
Introduction to the Functional Web Framework in Spring 5 by Eugen (Baeldung)
Dysfunctional programming in Java 5 : No Exceptions by john mcclean
Imperative Loop or Functional Stream Pipeline? Beware of the Performance Impact! by Lukas Eder
Bringing Reactive To Enterprise Java developers by Thomas Segismont and Julien Ponge
Reactive Microservices using RSocket by Ryland Degnan
Understanding Spring Reactive by Naveen Katiyar
Exploring Reactive Programming in Java by Miro Cupak
Given suspended method that throws an Exception (in this case suspended contrived example where the first call always fails, but subsequent calls succeed).
int times =0;
public String exceptionalFirstTime(String input) throws IOException{
if(times==0){
times++;
throw new IOException();
}
return input + " world";
}
We can make use of FluentFunctions to retry the method twice 500ms apart.
FluentFunctions.ofChecked(this::exceptionalFirstTime)
.println()
.retry(2,500)
.apply("hello");
And the output will look something like this (thanks to the println operator).
(fluent-function-Parameter[hello])
java.io.IOException
at com.aol.cyclops2-react.functions.fluent.FunctionsTest.exceptionalFirstTime(FunctionsTest.java:95)
...
(fluent-function-Parameter[hello])
(fluent-function-Result[hello world])
Alternatively we could decide to recover from the thrown exception.
FluentFunctions.ofChecked(this::exceptionalFirstTime) .recover(IOException.class, in->in+"boo!") .println() .apply("hello ")…
Architecture @ Verizon Media. Maintainer of Cyclops. Twitter @johnmcclean_ie