KEMBAR78
Introduction To Vavr: A Functional Java Library | PPTX
Presented By: Deepak Mehra(Sr.Software Consultant)
Introduction to Vavr : A functional
Java library
Agenda
01
● What is Vavr?
● Prerequisite for Vavr
● Tuples
● Functions
● Values
● Collections
● Pattern Matching
● Demo
What is Vavr?
01
● Vavr is essentially a functional Java library which aims to extend the features of Java8 and helps to
reduce the amount of code and increase code quality.
● It provides collections, functional interfaces for error handling, concurrent programming, and
pattern matching. There are many other modules which revolves around vavr core.
● Also, It provides immutable data structures(cannot be modified once created), persistent data
structures(does preserve the previous version of itself when being modified), functional data
structures(also known as purely functional data structures), they are immutable and persistent.
Prerequisite for Vavr
01
● Java8
● Maven/Gradle
Tuples
01
You must have seen Tuples in Scala if you have ever worked on Scala. Tuple is nothing but a data
structure which will have fixed number of elements and can hold objects of heterogeneous
types(different types).
Functions
01
In Java8 we have Function which accepts one parameter and BiFunction which accepts two
parameters and gives output whereas vavr provides functions with upto a limit of 8 parameters.
The functional interfaces are like this Function0, Function1, Function2 and so on.
Values
01
● Option
● Try
● Lazy
● Either
● Future
● Collections
● Pattern Matching
Values
01
An expression which can be further evaluated for calculations or other. In Java, this can be
expressed by making the state of an object final and call it immutable. Vavr’s functional value
abstracts over immutable objects. If variable and objects are immutable their state is unmodifiable
and unchangeable which provides thread safety. So, we can share the variable without any fear in
a multi threaded environment.
Option
01
Option – We saw Option in Scala language. However, in Java something similar was introduced
with the help Optional class which behaves pretty much similar to Scala’s Option. Instances of
Option are either an instance of Some or None. Honestly speaking I don’t see much difference in
Scala’s Option and Java’s Optional.
Try
01
Try – Try is nothing but a container that represents a computation that may either result in an
exception or return a value. This looks similar to Either but it is different than that. Instances of Try,
are either Success or Failure
Lazy
01
Lazy – Lazy is a container which will represent a lazy evaluated value. Lazy is memoized already, it
evaluates only once and next time onwards returns from a cache.
Either
01
Either – An instance of Either is either an instance of Left or Right. A common use of Either is as an
alternative to Option for dealing with possible missing values. You can relate it Option as well, so
Left is considered to be None type and Right is considered to be Some type. Instance Right will
hold success and Left will hold failure.
Future
01
Future – A Future represents the result of an asynchronous computation. When the asynchronous
task is created, a future object is returned
There is a get() with Futures which will compute the result from Future but it is blocking in nature. It
also has isComplete() which returns a boolean value to mention if a future has been completed.
A future has two states: pending and completed
Future
01
Pending – The computation is on going. A pending future may be completed or cancelled.
Completed – The computation finished successfully with a result, failed with an exception or
cancelled.
Java8 already has a rich set of APIs for future. Future has been there since Java5 and it has
improved with time. Java8 has provided improvements to it. Now, we have
CompletableFuture(class) and CompletionStage(interface) with several callback methods.
I personally love Java8’s future API. Vavr has also introduced future class in the library with some
additional feature which we can play around with.
Collections
01
Collections – In vavr library a new collection library has been introduced to meet the functional
requirements, which is immutable in nature. Vavr library will essentially reduce the boilerplate code
which we will see going forward. This new collection is essentially based on java.lang.Iterable.
List in Vavr – Vavr list is essentially an immutable linked list. Mutations will result in new instances.
Pattern Matching
01
Pattern matching was not introduced as part of Java8, although we have worked with pattern
matching in Scala but Java never had it. Vavr library has support for pattern matching, now you can
also use pattern matching in Java8.
In pattern matching match is essentially an expression which yields some result. Also, pattern
matching is really a great feature to save a lot of time as it avoids to write if else branches and the
code looks more clean and readable.
Vavr library provides a match API which is pretty much similar to Scala’s pattern matching.
Pattern Matching
01
Pattern matching was not introduced as part of Java8, although we have worked with pattern
matching in Scala but Java never had it. Vavr library has support for pattern matching, now you can
also use pattern matching in Java8.
In pattern matching match is essentially an expression which yields some result. Also, pattern
matching is really a great feature to save a lot of time as it avoids to write if else branches and the
code looks more clean and readable.
Vavr library provides a match API which is pretty much similar to Scala’s pattern matching.
References
01
● https://www.vavr.io/vavr-docs/
● https://github.com/vavr-io/vavr
● https://blog.knoldus.com/introduction-to-vavr-a-functional-library-for-java/
● https://blog.knoldus.com/functional-java-how-to-do-pattern-matching-in-java/
Thank You !

Introduction To Vavr: A Functional Java Library

  • 1.
    Presented By: DeepakMehra(Sr.Software Consultant) Introduction to Vavr : A functional Java library
  • 2.
    Agenda 01 ● What isVavr? ● Prerequisite for Vavr ● Tuples ● Functions ● Values ● Collections ● Pattern Matching ● Demo
  • 3.
    What is Vavr? 01 ●Vavr is essentially a functional Java library which aims to extend the features of Java8 and helps to reduce the amount of code and increase code quality. ● It provides collections, functional interfaces for error handling, concurrent programming, and pattern matching. There are many other modules which revolves around vavr core. ● Also, It provides immutable data structures(cannot be modified once created), persistent data structures(does preserve the previous version of itself when being modified), functional data structures(also known as purely functional data structures), they are immutable and persistent.
  • 4.
    Prerequisite for Vavr 01 ●Java8 ● Maven/Gradle
  • 5.
    Tuples 01 You must haveseen Tuples in Scala if you have ever worked on Scala. Tuple is nothing but a data structure which will have fixed number of elements and can hold objects of heterogeneous types(different types).
  • 6.
    Functions 01 In Java8 wehave Function which accepts one parameter and BiFunction which accepts two parameters and gives output whereas vavr provides functions with upto a limit of 8 parameters. The functional interfaces are like this Function0, Function1, Function2 and so on.
  • 7.
    Values 01 ● Option ● Try ●Lazy ● Either ● Future ● Collections ● Pattern Matching
  • 8.
    Values 01 An expression whichcan be further evaluated for calculations or other. In Java, this can be expressed by making the state of an object final and call it immutable. Vavr’s functional value abstracts over immutable objects. If variable and objects are immutable their state is unmodifiable and unchangeable which provides thread safety. So, we can share the variable without any fear in a multi threaded environment.
  • 9.
    Option 01 Option – Wesaw Option in Scala language. However, in Java something similar was introduced with the help Optional class which behaves pretty much similar to Scala’s Option. Instances of Option are either an instance of Some or None. Honestly speaking I don’t see much difference in Scala’s Option and Java’s Optional.
  • 10.
    Try 01 Try – Tryis nothing but a container that represents a computation that may either result in an exception or return a value. This looks similar to Either but it is different than that. Instances of Try, are either Success or Failure
  • 11.
    Lazy 01 Lazy – Lazyis a container which will represent a lazy evaluated value. Lazy is memoized already, it evaluates only once and next time onwards returns from a cache.
  • 12.
    Either 01 Either – Aninstance of Either is either an instance of Left or Right. A common use of Either is as an alternative to Option for dealing with possible missing values. You can relate it Option as well, so Left is considered to be None type and Right is considered to be Some type. Instance Right will hold success and Left will hold failure.
  • 13.
    Future 01 Future – AFuture represents the result of an asynchronous computation. When the asynchronous task is created, a future object is returned There is a get() with Futures which will compute the result from Future but it is blocking in nature. It also has isComplete() which returns a boolean value to mention if a future has been completed. A future has two states: pending and completed
  • 14.
    Future 01 Pending – Thecomputation is on going. A pending future may be completed or cancelled. Completed – The computation finished successfully with a result, failed with an exception or cancelled. Java8 already has a rich set of APIs for future. Future has been there since Java5 and it has improved with time. Java8 has provided improvements to it. Now, we have CompletableFuture(class) and CompletionStage(interface) with several callback methods. I personally love Java8’s future API. Vavr has also introduced future class in the library with some additional feature which we can play around with.
  • 15.
    Collections 01 Collections – Invavr library a new collection library has been introduced to meet the functional requirements, which is immutable in nature. Vavr library will essentially reduce the boilerplate code which we will see going forward. This new collection is essentially based on java.lang.Iterable. List in Vavr – Vavr list is essentially an immutable linked list. Mutations will result in new instances.
  • 16.
    Pattern Matching 01 Pattern matchingwas not introduced as part of Java8, although we have worked with pattern matching in Scala but Java never had it. Vavr library has support for pattern matching, now you can also use pattern matching in Java8. In pattern matching match is essentially an expression which yields some result. Also, pattern matching is really a great feature to save a lot of time as it avoids to write if else branches and the code looks more clean and readable. Vavr library provides a match API which is pretty much similar to Scala’s pattern matching.
  • 17.
    Pattern Matching 01 Pattern matchingwas not introduced as part of Java8, although we have worked with pattern matching in Scala but Java never had it. Vavr library has support for pattern matching, now you can also use pattern matching in Java8. In pattern matching match is essentially an expression which yields some result. Also, pattern matching is really a great feature to save a lot of time as it avoids to write if else branches and the code looks more clean and readable. Vavr library provides a match API which is pretty much similar to Scala’s pattern matching.
  • 18.
    References 01 ● https://www.vavr.io/vavr-docs/ ● https://github.com/vavr-io/vavr ●https://blog.knoldus.com/introduction-to-vavr-a-functional-library-for-java/ ● https://blog.knoldus.com/functional-java-how-to-do-pattern-matching-in-java/
  • 19.