0% found this document useful (0 votes)
352 views74 pages

Practical Vavr Sample

Uploaded by

Kebab Tvrtkić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
352 views74 pages

Practical Vavr Sample

Uploaded by

Kebab Tvrtkić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Practical Vavr

Alexandre Grison
This book is for sale at http://leanpub.com/practical-vavr

This version was published on 2020-12-07

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.

© 2020 Alexandre Grison


Tweet This Book!
Please help Alexandre Grison by spreading the word about this book on Twitter!
The suggested tweet for this book is:
I just bought Practical Vavr by Alexandre Grison. #practical-vavr #java @vavr_io
The suggested hashtag for this book is #practical-vavr.
Find out what other people are saying about the book by clicking on this link to search for this
hashtag on Twitter:
#practical-vavr
Contents

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

Mapping and nulls or other Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12


Transforming an Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

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

Head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42


Getting the size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Retrieving the keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Retrieving the values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Dropping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Taking elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Sorting Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Mapping Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Sliding over Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Scanning entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Grouping elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Merging two Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Reversing entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Keeping distinct entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Folding and reducing Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45


Zipping entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Partitioning Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Interrop with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using with collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

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

Vavr and Kotlin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61


Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Either . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Vavr and Property Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63


Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
FizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Practical Vavr

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.

• Daniel Dietrich, Creator of Vavr


https://twitter.com/danieldietrich
Vavr
Vavr (formerly called Javaslang) is a functional library for Java.
As stated from the official repository of Vavr¹:

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

You can find me at grison.me⁶ and on Twitter at @algrison⁷.

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().

1 var t = Tuple.of("Alex", 36); // Tuple2

Notice that this Tuple contains two different types, one String and one Integer.

1 record Customer(String name, int age) {}


2
3 var t = Tuple.of(42, "Hello", new Customer("Alex", 36)); // Tuple3
4 t._1; // 42
5 t._2; // "Hello"
6 t._3; // Customer(name = "Alex", age = 36)
7
8 // you can also use _N as a method
9 t._1(); // 42
10 t._2(); // "Hello"
11 t._3(); // Customer(name = "Alex", age = 36)

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.

1 var t = Tuple.of(new SimpleEntry(42, "foo"));

Creating Tuples from Iterables


You can create an instance of Tuple with an Iterable of tuples using the sequenceN method, where
N refers to the size of the Tuple.
Tuple 7

1 List<Tuple2<Integer, String>> list = Arrays.asList(


2 Tuple.of(1, "foo"), Tuple.of(2, "bar"));
3
4 var t = Tuple.sequence2(list); // Tuple2(Seq(1, 2), Seq("foo", "bar"))

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:

1 var t = Tuple.of("Alex", 36);


2 var newTuple = t.map(String::toUpperCase, age -> ++age);
3 newTuple._1; // "ALEX"
4 newTuple._2; // 37
5 // t stays unchanged
6 t._1; // "Alex"
7 t._2; // 36

or one with N-arities.

1 var t = Tuple.of("Alex", 36);


2 var newTuple = t.map((name, age) -> Tuple.of(name.toUpperCase(), ++age));
3 newTuple._1; // "ALEX"
4 newTuple._2; // 37
5 // t stays unchanged
6 t._1; // "Alex"
7 t._2; // 36

If you only need to map a specific Tuple element you can use mapN() where N is the element position.
Tuple 8

1 var t = Tuple.of("Alex", 36);


2 var newTuple = t.map2(age -> ++age);
3 newTuple._1; // "Alex"
4 newTuple._2; // 37
5 // t stays unchanged
6 t._1; // "Alex"
7 t._2; // 36

Updating a Tuple
Vavr offers an updateN() method, where N is the element position to update.

1 var t = Tuple.of("Alex", 36);


2 var newTuple = t.update2(50);
3 newTuple._1; // "John"
4 newTuple._2; // 50

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.

1 var t = Tuple.of("Alex", 36)


2
3 var hello = t.apply((name, age) -> "My name is " + name + " and I'm " + age +
4 " years old");
5 hello; // "My name is Alex and I'm 36 years old"

Getting the size


If you want to retrieve the size of a Tuple instance, just ust the arity method.

1 var t = Tuple.of("Alex", 36);


2 t.arity(); // 2

Growing Tuples
Let’s say you want to grow your Tuple from 2 to 3 elements, then use the append method.
Tuple 9

1 record Address(String street, String city) {}


2
3 var t = Tuple.of("Alex", 36);
4 var newTuple = t.append(new Address("rue Serpenoise", "Metz"));
5 newTuple.arity(); // 3
6 newTuple._3; // Address(street = "rue Serpenoise", city = "Metz")

Vavr also offers the possibility to concat tuples, it’s like append but with other tuples.

1 record Address(String street, String city) {}


2 record Nationality(String nationality, String birthPlace) {}
3
4 var t = Tuple.of("Alex", 36);
5 var p = Tuple.of(new Address("rue Serpenoise", "Metz"),
6 new Nationality("French", "Paris"));
7 var newTuple = t.concat(p);
8 newTuple.arity(); // 4
9 newTuple._3; // Address(street = "rue Serpenoise", city = "Metz")
10 newTuple._4; // Nationality(street = "French", birthPlace = "Paris")

Converting to a sequence
Instances of Tuple can be converted to a Seq using toSeq() (with a List implementation).

1 var t = Tuple.of("foo", "bar", "bazz");


2 t.toSeq(); // Seq(foo, bar, bazz)

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.

1 int h = Tuple.hash("foo", "bar"); // ...

Which is equivalent to:

1 (31 * (31 + Objects.hashCode("foo")) + Objects.hashCode("bar"))


Tuple 10

Tuples and Maps


Tuples are used by Vavr to represent and work with entries in a Map collection, thus you can create
a Map from a Tuple, iterate on Tuples representing each entry of the Map, get the first and last
elements as a Tuple, and much more, but we’ll see about this in the Collections chapter.
In its io.vavr.control package, Vavr provide useful values like Option, Either and Try. These
values share similar APIs and as such, some operations are valid on these 3 types.
You will then find similarities in the following sections.
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 Option
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Conditionally 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.

Creating an Option from a sequence


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Checking the status of 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 Option holding null


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running side effects


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Option 12

Getting the value out of 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.

Mapping and nulls or other 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.

Using with collectors


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.

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.

Creating an Either from a sequence


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Checking the status of an Either


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running side effects


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Getting the value out of 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.

Mapping an Either (transforming what’s inside)


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Using with collectors


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.

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.

Using a Try to run a procedure


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Checking the status of a Try


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running side effects


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Chaining side effects


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

Getting the value out of a Try


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

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.

Mapping and null


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Making a Success from a Failure


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Try 17

Recovering from errors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Working with resources


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Using with collectors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Lazy
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.

Getting the value


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Knowing if a Lazy has been evaluated


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.

Running a side effect


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Lazy 19

Transforming to another domain


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.

Retrieve a Future state


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Retrieving a Future’s value


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running side effects


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

Finding a Future within a sequence


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Finding the first completed Future within a sequence


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running tasks
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Accessing the underlying Executor


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Providing a custom Executor


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Mapping the Future


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.

Recovering from errors


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.

Using with collectors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Match
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.

Combining and applying validations


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Retrieve the status of 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.

Running a side effect


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.

Getting the arity


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Using Tuples as 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.

Performance characteristics of Vavr Collections


These information are extracted from the Vavr Javadoc⁸.
Time complexity of sequential operations

head() tail() get(int) update(int, prepend(T) append(T)


T)
Array const linear const const linear linear
CharSeq const linear const linear linear linear
Iterator const const — — — —
List const const linear linear const linear
Queue const const linear linear const const
PriorityQueue log log — — log log
Stream const const linear linear constˡʸ constˡʸ
Vector const const const const const const

Time complexity of Map/Set operations

contains/Key add/put remove min


HashMap const const const linear
HashSet const const const linear
LinkedHashMap const linear linear linear
LinkedHashSet const linear linear linear
Tree log log log log
TreeMap log log log log
TreeSet log log log log

• const · constant time


• const · amortized constant time, few operations may take longer
• const · effectively constant time, depending on assumptions like distribution of hash keys
• constˡʸ · lazy constant time, the operation is deferred
• log · logarithmic time
• linear · linear time
⁸https://javadoc.io/doc/io.vavr/vavr/latest/io/vavr/collection/package-summary.html
Collections 31

Vavr collections regarding Java collections

Vavr Java Description


Array Object[] Traversable wrapper for Object[]
CharSeq - A rich String wrapper
Iterator java.util.Iterator Compositional replacement for Java’s
Iterator
List - Immutable eager sequence of
elements, using Nil and Cons
Queue java.util.ArrayDeque Immutable Queue storing elements
allowing FIFO retrieval
PriorityQueue java.util.PriorityQueue Immutable Priority Queue
Stream java.util.Stream Immutable Stream as a lazy sequence
of elements which may be infinitely
long
Vector java.util.ArrayList The default Seq implementation that
provides effectively constant time
access to any element
HashMap java.util.HashMap Immutable HashMap based on a
HAMT
HashSet java.util.HashSet Immutable HashSet
LinkedHashMap java.util.LinkedHashMap An immutable LinkedHashMap
implementation that has predictable
(insertion-order) iteration
LinkedHashSet java.util.LinkedHashSet An immutable HashSet
implementation that has predictable
(insertion-order) iteration
Tree - General Tree interface
TreeMap java.util.TreeMap Immutable SortedMap based on a
Red/Black Tree
SortedSet java.util.SortedSet Immutable SortedSet based on a
Red/Black Tree

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.

Removing and filtering elements


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.

Slices and sub sequences


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sequences 33

Head and tail


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.

Inserting between 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.

Checking if starting or ending


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Folding and reducing sequences


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sequences 34

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.

Sliding over sequences


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sequences 35

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.

Keeping distinct elements


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sequences 36

Generating cross products


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.

Finding longest segments


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.

Minimum and maximum


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.

Using with collectors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Sets
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.

Removing and filtering elements


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

Head and tail


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Getting the size


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 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.

Keeping distinct elements


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.

Minimum and maximum


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.

Interrop with Java


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Using with collectors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
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 Map
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Adding entries to 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.

Removing and filtering elements


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.

Head and tail


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Maps 43

Getting the size


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Retrieving the keys


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Retrieving the values


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

Sliding over Maps


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

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.

Merging two Maps


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.

Keeping distinct entries


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Folding and reducing Maps
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.

Interrop with Java


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Using with collectors


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Streams
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.

Removing and filtering elements


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.

Slices and sub sequences


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Streams 47

Head and tail


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.

Checking if starting or ending


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.

Sliding over streams


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.
Streams 48

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.

Keeping distinct elements


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Generating cross products


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.

Try: a real world example


Imagine you are writing a service which can be seen as a pipeline, like:

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

that register may fail and return None.


You could return a Try also but in this case any error is correctly logged (imagine going into ELK)
so there’s no need for the caller to know exactly what went wrong, just that it went wrong.

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

The Gilded Rose Kata


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

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.

Read the spec, then create separated functions


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Enhance the Item class


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

And now, Vavr


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.

From HTTP to the Database, and back


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 56

Initiating the project


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Configuring Vavr Jackson


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

About the application


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.

The Repository layer


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The Service layer


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The Controller layer


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

The REST API

This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The testing sample


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Running the application


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.

Analysing football data


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.

Playing with data


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

First player to score for FC Metz


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Last player to score for FC Metz


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Total number of goals


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The best striker


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

The largest victory


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The largest defeat


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The total number of points in Ligue 1


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Find who we won, lose and draw against


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Find the longest undefeated streak


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Average goals per match


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Do we score more in the first or second half


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

The youngest striker


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 60

Are we better at home or away


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Constructing the evolution of score & goals


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.

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.

Day 01: Naive implementation


This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

Day 01: With Vavr


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
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.

Rest of the API

This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/practical-vavr.

You might also like