I ❤ VAVR

JAVING
Javarevisited
Published in
4 min readMar 28, 2022

On one of my first jobs as a Java developer back in 2012, I had the luck to work on a team that was very progressive with coding practices. They introduce me to pair programming and test-driven development among others. I remember back then I read a little book called “Java 8 for the really impatient”.

I was very interested in Java 8 and functional programming.
So much that after work when I arrived home I would try refactoring chunks of code from some git repositories from a declarative style of Java to a functional Java 8 style.

In-office I would talk to my colleagues about and we did ask management if was it possible to early adopt Java 8 and upgrade our apps. Unfortunately upgrading to java 8 so quickly was not a priority for the business. But the disappointment didn’t last long. One colleague from another team told us that in their team they had the same reply from the business but they started using VAVR.io instead of upgrading to Java 8.

This was great news and since there was no kind of restriction on libraries we immediately adopted the VAVR library. As days and weeks passed I would learn to program functionally using VAVR. I really loved it and it would not be until nearly 2 years later that I would code Java 8 professionally for the first time.

VAVR which was years ago called JavaSlang, is a Java api that brings functional programming capabilities to your code and also provides a great immutable collections api. In this article I don’t want to talk a lot of theory about VAVR, I just want to show code in Java 8 and it’s VAVR equivalent so that you can see how nice it was to work with this library. I hope you like it.

Function N types
Java 8 supports Function and ByFunction but Vavr supports Function(N) types that allows to have up to 8 parameters.

Composed Functions
With VAVR we can make compositions of functions. Very nice feature that allows us to extend functionality in a maintainable way.

Lifting
If a function within a composed function throws an exception we could prevent the exception and instead return Option.none. This is very useful when composing functions that use third-party libraries and can return exceptions.

Partial application
We can partially apply a function by passing fewer parameters than the ones it requires.

Currying
Currying allows us to decompose a function of multiple arguments into a succession of functions of a single argument.

Memoization / Idempotency
If a function is called with the same parameters the result should be the same. Memoization allows us to easily implement caching in a function.

Option
In my opinion VAVR’s Option is superior to it’s Java counterpart Optional.
Here just some reasons:

  • In VAVR Option is serializable as opposed to Optional in Java 8 which is not.
  • Option in VAVR supports peek which allows us to perform an action if there’s something.
  • Option is interoperable with java 8 optional
  • In vavr a call to Option.map() can result in Some(null) this could lead to a NullPointerException. Some people don’t like this but it actually forces you to pay attention to possible occurrences of null and deal with them accordingly instead of unknowingly accepting them. The correct way to deal with occurrences of null is to use flatMap.

Option is a wrapper of values, if used correctly, we can avoid null checks and also NullPointerException.

This was just a little example of vavr’s Option. But the power of vavr’s Option is much larger I would need another article just to talk about it. But I would like to share just a link to a git repo where I have much more Option examples so that you can see what other things it can do.

Try
Try is an alternative way of exception handling which is much flexible than classic exception handling in Java:

  • We can return Try from method’s to postpone an execution also it’s syntax is very intuitive.
  • We can graciously provide alternative execution paths in case of errors.
  • If a method that we called return multiple exceptions but we just want to selectively react to one of them thanks to Try’s method recoverWith()
  • Combining Try.sequence and flatmap we can extract the values from a list of Try.

Lazy Initialised Values
Regardless we are calling it multiple times, a Lazy initialised value will compute only once.

Collections
Vavr introduces a very powerful immutable collections api. It will take me a much broader article to explain more about all the vavr collection features but let’s just have a little example of some useful collections features in vavr.

Tuples
Tuples are groups of elements. Java 8 supports pairs but vavr goes one step further and allows tuples. The values of Tuples are accessed using the values _1, _2, etc… Maybe not most name friendly we must admit. The max tuple size is 8 elements.

Painless Checked exceptions
This is a classic problem when using lambdas and checked exceptions. We have to embed the catch block in such an ugly way, alternatively we can refactor, bla bla… But it just doesn’t look nice.

In vavr we can do something like this so that we can have painless checked exceptions.

Pattern matching
With vavr we can do pattern matching as an alternative to switch statements

Thanks for your time reading I hope you liked this article and/or found it useful. If you enjoyed this kind off content, I would very much appreciate if you give me some claps, follow and share on your social media.
I am aiming to publish 1 article monthly.

--

--

JAVING
Javarevisited

The present continuous form of “to program in Java”.