Practical Vavr Sample
Practical Vavr Sample
Alexandre Grison
This book is for sale at http://leanpub.com/practical-vavr
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Practical Vavr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Vavr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Why . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
This book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Why . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Code samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
About me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Creating Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Creating Tuples from Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Mapping a tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Updating a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Transforming Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Getting the size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Growing Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Converting to a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Hashing objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Tuples and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Creating an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Conditionally creating an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Creating an Option from a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Checking the status of an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Creating an Option holding null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Running side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Getting the value out of an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Filtering an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Mapping an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
CONTENTS
Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Creating an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Creating an Either from a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Checking the status of an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Running side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Getting the value out of an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Filtering an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Swaping an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Mapping an Either (transforming what’s inside) . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Creating a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using a Try to run a procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Checking the status of a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Running side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chaining side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Getting the value out of a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Filtering a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Making an Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Making a Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Mapping a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Mapping and null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Making a Success from a Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Recovering from errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Working with resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Creating a Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Getting the value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Knowing if a Lazy has been evaluated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Filtering a Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Mapping on a Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Running a side effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Transforming to another domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Deprecation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CONTENTS
Creating a Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Retrieve a Future state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Retrieving a Future’s value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Running side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Canceling a Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Awaiting termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Finding a Future within a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Finding the first completed Future within a sequence . . . . . . . . . . . . . . . . . . . . . . . 21
Running tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Accessing the underlying Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Providing a custom Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Mapping the Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Folding futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Reducing Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Filtering Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Zipping Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Recovering from errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Try Interrop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Java Interrop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Creating a Match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Adding cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating a validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Combining and applying validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Retrieve the status of a Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Swapping a Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Running a side effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Folding a Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Mapping a Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Creating functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Lifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Partial application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
CONTENTS
Reversing parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Getting the arity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using Tuples as parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Sneaky Throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Recovering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Performance characteristics of Vavr Collections . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Vavr collections regarding Java collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Creating a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Appending to a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Updating an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Removing and filtering elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Finding elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Slices and sub sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Dropping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Inserting between elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Taking elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Checking if starting or ending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Folding and reducing sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Partitioning sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Patching sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Replacing an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Splitting sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Sorting sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Shuffling sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Sliding over sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Scanning sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Rotating sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Reversing sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Generating permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Generating combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Grouping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Keeping distinct elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Generating cross products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Zipping sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Finding longest segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
CONTENTS
Product of elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Minimum and maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Averaging elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Flattening sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Making Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Java Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Creating a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Creating ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Appending to a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Updating an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Removing and filtering elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Finding elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Getting the size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Dropping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Taking elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Sorting sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Partitioning sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Sliding sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Scanning sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Grouping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Union of sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Intersection of sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Difference of sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Keeping distinct elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Zipping sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Summing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Product of elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Minimum and maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Averaging elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Making Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Interrop with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Creating a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Adding entries to a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Updating an entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Removing and filtering elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Finding elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
CONTENTS
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Creating a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Appending to a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Updating an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Removing and filtering elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Finding elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Slices and sub sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Dropping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Taking elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Checking if starting or ending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Splitting streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Sorting streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Sliding over streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Scanning streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Rotating streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Reversing streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Generating permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Generating combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Keeping distinct elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Generating cross products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Zipping streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
CONTENTS
Vavr in action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Try: a real world example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
The Gilded Rose Kata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
From HTTP to the Database, and back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Analysing football data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Advent Of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Practical Vavr is a clearly structured overview of basic functional programming concepts with Vavr.
There are many examples in this book to help you understand most of the existing Vavr APIs.
I like the pointers Alexandre gives about my design decisions and how he puts topics in a larger
context.
This is a book I am recommending to all Vavr users. It is a great introduction to Vavr for beginners
and an everyday reference for experts.
Vavr is an object-functional language extension to Java 8, which aims to reduce the lines of
code and increase code quality. It provides persistent collections, functional abstractions
for error handling, concurrent programming, pattern matching, and much more.
Vavr fuses the power of object-oriented programming with the elegance and robustness of
functional programming. The most interesting part is a feature-rich, persistent collection
library that smoothly integrates with Java’s standard collections.
Because Vavr does not depend on any libraries (other than the JVM) you can easily add
it as standalone .jar to your classpath.
To sum up, it’s a Java library that helps to reduce the amount of code and to increase the robustness,
using concepts from functional programming, immutable values, and control structures to operate
on these values.
Vavr has been originally developed by Daniel Dietrich² and was released in March 2014.
Why
Have you heard about Functional Programming, or have you been attracted by it but still need to
use Java? Do you want to write more robust code, but in a different way, a more elegant way? Do
you want a library that gives you all these well-thought building blocks in a well-organized API?
Well, Vavr is the library you need. It can improve your code, give you confidence in writing it, make
you design your business code better, and can prevent you from falling into programming traps and
all kinds of exceptions.
How
Using Vavr
The latest released version as the date I’m writing these lines is 0.10.3.
If you are using Maven just add the following dependency to your pom.xml file
¹https://github.com/vavr-io/vavr
²https://twitter.com/danieldietrich
Vavr 3
1 <dependencies>
2 <dependency>
3 <groupId>io.vavr</groupId>
4 <artifactId>vavr</artifactId>
5 <version>0.10.3</version>
6 </dependency>
7 </dependencies>
If you are using Gradle, just add the following dependency to your build.gradle file
1 dependencies {
2 compile "io.vavr:vavr:0.10.3"
3 }
And finally, if you’re not using any build system, you can just download the latest version and drop
the JAR in your classpath.
That’s it, you’re ready to rock some code in your favorite code editor or IDE and benefit instantly
from the features of Vavr.
This book
Why
I have been using Vavr before it was called Vavr, I discovered it beginning of 2015, and have been
using it pretty extensively since then.
As a Tech Lead and Software Craftsman in my day to day job, I like to promote this library to help
developers write less and safer code with better quality, understand functional programming idioms
and how to apply them in their work.
I’ve been teaching this library for multiple years, and developers always tend to have the same set
of questions on how to get going with it, when to use it, also how and why to use it.
This book will cover what I think are the best parts of Vavr, how the API works, and what it has to
offer in your real world day-to-day programming.
Code samples
Code example will be using Java 15 with preview enabled so that we can benefit from features like
the new instanceof syntactic sugar, and from records to avoid Lombok when not necessary.
The code is available on my github/agrison³.
About me
My name is Alexandre, I am a Software Engineer from Metz⁴ in France and working as a Tech Lead
and Software Craftsman in Luxembourg⁵.
In my spare time, I like to code in various languages including Java (with Vavr), Clojure/Clojure-
Script, Kotlin, JavaScript, Go, Python, OCaml, and a few others. I’m interested in both backend and
frontend having practiced them extensively.
From APIs to mobile, through the cloud and databases, preferably using a functional programming
language.
Besides coding, I am passionate about my wife Jessica, and my daughter Eva, I also love to travel
and practice photography.
³https://github.com/agrison
⁴https://en.wikipedia.org/wiki/Metz
⁵https://en.wikipedia.org/wiki/Luxembourg
This book 5
The cover
The book’s cover is a picture of Metz at sunrise as seen from the Plan d’eau.
It features the Cathedrale Saint Etienne de Metz on the left, which is the Cathedral having one of
the highest naves in the world and the largest expanse of stained glass in the world.
Besides, on the right you’ll see a Torii - a traditional Japanese gate - which was installed around
1985 (the year I was born) during a Japanese exposition in Metz.
If you happen to be visiting Metz, don’t hesitate to drop me a message :)
⁶https://grison.me
⁷https://twitter.com/algrison
Tuple
Let’s say you want to create a collection of heterogeneous elements. A small collection going from
0 to 8 elements.
Of course, you can create a custom class or record for it, however, sometimes you don’t need or
want to.
Here comes the Tuple type. Vavr’s Tuples are immutable and are of type Tuple0, Tuple1, Tuple2, …,
Tuple8.
Creating Tuples
In order to create a Tuple, just use the factory method Tuple.of().
Notice that this Tuple contains two different types, one String and one Integer.
In this example, the Tuple contains three elements, one Integer, one String, and one Customer.
Finally, you can also create a Tuple2 from a Map.Entry.
Note that it returns a TupleN<Seq<T1>, ...> but implementation related the Seq are of type Stream.
Accessing elements
Each TupleN implementation will provide a _N accessors to let you get the element. You can also use
the _N() method to access it.
Mapping a tuple
There are two ways to map a function, providing N functions:
If you only need to map a specific Tuple element you can use mapN() where N is the element position.
Tuple 8
Updating a Tuple
Vavr offers an updateN() method, where N is the element position to update.
It is similar to mapN except that mapN takes a Function whereas updateN takes directly the value to
update the tuple element with.
Transforming Tuples
If you want to transform a Tuple to a new type, there’s the apply method for that.
Growing Tuples
Let’s say you want to grow your Tuple from 2 to 3 elements, then use the append method.
Tuple 9
Vavr also offers the possibility to concat tuples, it’s like append but with other tuples.
Converting to a sequence
Instances of Tuple can be converted to a Seq using toSeq() (with a List implementation).
Hashing objects
This comes as a practical utility, the Tuple type has a method hash which will compute a hash of all
the given objects.
Creating an Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Filtering an Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Mapping an Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Transforming an Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating an Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Filtering an Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Either 14
Swaping an Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Try
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Finally
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Try 16
Filtering a Try
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Making an Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Making a Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Mapping a Try
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Lazy
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Filtering a Lazy
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Mapping on a Lazy
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Deprecation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Future
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Future
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Canceling a Future
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Awaiting termination
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Future 21
Running tasks
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Folding futures
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Future 22
Reducing Futures
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Filtering Futures
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Zipping Futures
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Try Interrop
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Java Interrop
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Match
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Adding cases
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Patterns
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Tuple
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Match 24
Try
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
List
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Predicates
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
allOf
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
anyOf
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
instanceOf
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
is
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Match 25
isIn
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
isNull
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
isNotNull
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
noneOf
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Swapping a Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Folding a Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Validation 27
Mapping a Validation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Functions
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating functions
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Constants
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Composition
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Lifting
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Partial application
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Currying
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Functions 29
Memoization
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Reversing parameters
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sneaky Throws
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Recovering
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Collections
Vavr provides purely functional collections based on the Traversable class.
Hierarchy
Sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a sequence
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Appending to a sequence
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Updating an element
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Finding elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Dropping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Padding
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Taking elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Partitioning sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Patching sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Replacing an element
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Splitting sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sorting sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Shuffling sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Scanning sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Rotating sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Reversing sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Generating permutations
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Generating combinations
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Grouping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Zipping sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Summing elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Product of elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Averaging elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sequences 37
Flattening sequences
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Making Strings
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Java Interop
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Set
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating ranges
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Appending to a Set
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Updating an element
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Finding elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sets 39
Dropping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Taking elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sorting sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Partitioning sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sliding sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sets 40
Scanning sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Grouping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Union of sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Intersection of sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Difference of sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Zipping sets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sets 41
Summing elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Product of elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Averaging elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Making Strings
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a Map
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Updating an entry
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Finding elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Dropping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Taking elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sorting Map
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Mapping Maps
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Maps 44
Scanning entries
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Grouping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Reversing entries
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Zipping entries
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Partitioning Maps
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Creating a sequence
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Appending to a Stream
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Updating an element
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Finding elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Dropping elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Taking elements
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Splitting streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sorting streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Scanning streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Rotating streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Reversing streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Generating permutations
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Generating combinations
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Zipping streams
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr in action
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
1. data in
2. transform (may have side effect)
3. data out
4. transform (may have side effect)
5. data out
6. repeat…
But what if during step 6 we need data which was computed during step 2?
We need to keep a context of the pipeline which will be passed during the execution of the pipeline.
Of course you don’t want to execute step 4 if step 2 failed.
Using Lombok, it’s really easy to create Java beans enriched with plenty of features which makes
the code easy to both write and read, this is our context.
Example
Let’s take a look at some sort of registration pipeline where we can imagine the business is the
following:
1. Given an id
2. Retrieve a user details: email, first name and password (not really secure ;-)
3. Register an account on Twitter
4. Authenticate on Twitter
5. Tweet Hello, world
6. Update the user details with the Twitter account id
7. Log something in case of success
8. Return the tweet URL
9. In case of error anywhere log something also
Simple code
A simple code example for this could be like the following:
Vavr in action 50
1 interface UserService {
2 User byId(String userId);
3 void updateTwitterAccount(String userId, String twitterId);
4 }
5
6 interface TwitterService {
7 TwitterAccount register(String email, String firstName, String password);
8 String authenticate(String email, String password);
9 Tweet tweet(String authToken, String message);
10 }
11
12 @RequiredArgsConstructor
13 class TwitterRegistrationService {
14 final UserService userService;
15 final TwitterService twitterService;
16 final BusinessLogger blog;
17
18 /**
19 * Register the given user on Twitter,
20 * post a hello world and return this tweet URL.
21 *
22 * @param userId the user id
23 * @return the tweet URL
24 */
25 public String register(String userId) {
26 try {
27 User user = userService.byId(userId);
28 if (user == null) {
29 blog.logErrorRegisteringTwitterAccount(id);
30 return null;
31 }
32
33 TwitterAccount account = twitterService.register(
34 user.email, user.firstName, user.password);
35 if (account == null) {
36 blog.logErrorRegisteringTwitterAccount(id);
37 return null;
38 }
39
40 String authToken = twitterService.authenticate(
41 user.email, user.password);
42 if (authToken == null) {
43 blog.logErrorRegisteringTwitterAccount(id);
Vavr in action 51
44 return null;
45 }
46
47 Tweet tweet = twitterService.tweet(authToken, "Hello, world!");
48 if (tweet == null) {
49 blog.logErrorRegisteringTwitterAccount(id);
50 return null;
51 }
52
53 userService.updateTwitterAccount(userId, account.id);
54
55 blog.logSuccessRegisteringTwitterAccount(id);
56
57 return tweet.url;
58 } catch (Exception e) {
59 blog.logErrorRegisteringTwitterAccount(id, e);
60 return null;
61 }
62 }
63 }
This code is easy to follow but has a lot of repetitions, first we could return Options so that the code
don’t have to check for null.
But we’ll rather look at how to use Try to solve this problem. Each time you see a check like if (foo
== null) we’ll use the Try monad to avoid it and chain the next computation.
Improved code
Let’s see how we can improve the code by just modifying the RegistrationService and creating
the context object.
Context
The context needs to store anything which is useful for the pipeline to execute, here we make use of
Lombok to avoid boilerplate and make things clearer:
Vavr in action 52
1 @Data
2 @Accessors(chain = true)
3 class Context {
4 String id, email, firstName, password;
5 String accountId, token, url;
6
7 public Context(User user) {
8 this.id = user.id;
9 this.email = user.email;
10 this.firstName = user.firstName;
11 this.password = user.password;
12 }
13 }
Note the usage of @Accessors which will help to write less code later on.
The Lombok annotations will help writing cleaner and concise code. Of course you could also use
MapStruct so that it can generate a Mapper from User to Context and avoid some manual code ;).
RegistrationService
1 @RequiredArgsConstructor
2 class TwitterRegistrationService {
3 final UserService userService;
4 final TwitterService twitterService;
5 final BusinessLogger blog;
6
7 /**
8 * Register the given user on Twitter,
9 * post a hello world and return this tweet URL.
10 *
11 * @param userId the user id
12 * @return the tweet URL if any
13 */
14 public Option<String> register(String userId) {
15 return userDetails(userId)
16 .flatMap(this::registerTwitter)
17 .flatMap(this::authenticate)
18 .flatMap(this::tweet)
19 .andThen(this::updateUserTwitterAccount)
20 .andThen(c -> blog.logSuccessRegisteringTwitterAccount(userId))
21 .onFailure(e -> blog.logErrorRegisteringTwitterAccount(userId, e))
22 .map(Context::getUrl)
23 .toOption();
Vavr in action 53
24 }
25
26 // Create a registration context based on the userId
27 Try<Context> userDetails(String userId) {
28 return Try.of(() -> userService.byId(userId)).map(Context::new);
29 }
30
31 // register a twitter account for the user
32 Try<Context> registerTwitterAccount(Context c) {
33 return Try.of(() -> twitterService.register(c.email, c.firstName, c.password\
34 ))
35 .map(account -> c.setAccountId(account.id));
36 }
37
38 // authenticate on twitter as the newly created user
39 Try<Context> authenticate(Context c) {
40 return Try.of(() -> twitterService.authenticate(c.email, c.password))
41 .map(c::setToken);
42 }
43
44 // tweet "Hello, world!" and retrieve the tweet URL
45 Try<Context> tweet(Context c) {
46 return Try.of(() -> twitterService.tweet(c.token, "Hello, world!"))
47 .map(tweet -> c.setUrl(tweet.url));
48 }
49
50 void updateUserTwitterAccount(Context c) {
51 return Try.run(() -> userService.updateTwitterAccount(c.id, c.accountId));
52 }
53 }
Each if (foo == null) from the original code has been replaced by its own function taking a
Context and returning a Try.
Each of these function will call another service be it the twitterService or the userService, finally
the register function uses all theses construct and create a pipeline of execution by using flatMap
to deal with the fact that the different functions return a Try and thus may fail.
Indeed, we want to short circuit and stop as soon as an error occurs, hopefully everything goes well
and the code reaches and execute the map(Context::getUrl) and return the tweet URL.
Almost the same amount of code (around 50 lines), but now the pipeline is clear, you can clearly see
what registering is about.
Besides, you avoid a big try catch and tell clearly to the consumer of your RegistrationService
Vavr in action 54
This pattern
I came with this pattern which plays really well with Vavr & Lombok APIs when I started to use
Vavr a lot, I don’t know if it has a name except (and I don’t care that much :-), it’s pure pragmatism,
that’s what I would have done if I was to use Clojure at work, just passing a map from step to step,
associng keys in it.
However in Java it’s more practical to define and use a custom object than using a Map<String,
Object>, that’s where Lombok comes just as needed to reduce the boilerplate.
Vavr in action 55
Requirements specifications
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
First Refactoring
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Git repository
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
The entities
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Validating inputs
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr in action 57
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Recap
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Git repository
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Player dataset
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Game dataset
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr in action 58
Java records
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Reading datasets
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Top 5 strikers
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr in action 59
Recap
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Git repository
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Advent Of Code
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Usage
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Tuple
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Either
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Try
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
List
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr and Kotlin 62
Set
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Map
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr and Property Testing
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Usage
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
FizzBuzz
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
API
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Arbitrary
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Gen
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Fixed one
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Vavr and Property Testing 64
Choosing
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Failing
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.