CompletableFuture

Observable

Functors

import java.util.function.Function; interface Functor<T> { <R> Functor<R> map(Function<T, R> f); }

map()

f

Functor<T>

map

R

map(x -> x)

Functor<T>

T

interface Functor<T,F extends Functor<?,?>> { <R> F map(Function<T,R> f); } class Identity<T> implements Functor<T,Identity<?>> { private final T value; Identity(T value) { this.value = value; } public <R> Identity<R> map(Function<T,R> f) { final R result = f.apply(value); return new Identity<>(result); } }

F

Identity

map

Identity<String> idString = new Identity<>("abc"); Identity<Integer> idInt = idString.map(String::length);

Identity<byte[]> idBytes = new Identity<>(customer) .map(Customer::getAddress) .map(Address::street) .map((String s) -> s.substring(0, 3)) .map(String::toLowerCase) .map(String::getBytes);

byte[] bytes = customer .getAddress() .street() .substring(0, 3) .toLowerCase() .getBytes();

java.util.Optional<T>

map()

class FOptional<T> implements Functor<T,FOptional<?>> { private final T valueOrNull; private FOptional(T valueOrNull) { this.valueOrNull = valueOrNull; } public <R> FOptional<R> map(Function<T,R> f) { if (valueOrNull == null) return empty(); else return of(f.apply(valueOrNull)); } public static <T> FOptional<T> of(T a) { return new FOptional<T>(a); } public static <T> FOptional<T> empty() { return new FOptional<T>(null); } }

FOptional<T>

null

FOptional

empty()

Identity

FOptional

FOptional

f

T

List

import com.google.common.collect.ImmutableList; class FList<T> implements Functor<T, FList<?>> { private final ImmutableList<T> list; FList(Iterable<T> value) { this.list = ImmutableList.copyOf(value); } @Override public <R> FList<?> map(Function<T, R> f) { ArrayList<R> result = new ArrayList<R>(list.size()); for (T t : list) { result.add(f.apply(t)); } return new FList<>(result); } }

T -> R

FList

customers

import static java.util.Arrays.asList; FList<Customer> customers = new FList<>(asList(cust1, cust2)); FList<String> streets = customers .map(Customer::getAddress) .map(Address::street);

customers.getAddress().street()

getAddress()

getAddress()

customer*.getAddress()*.street()

map

list

map

Stream

list.stream().map(f).collect(toList())

java.util.stream.Stream<T>

Future

Promise

Promise<T>

Promise<Customer> customer = //... Promise<byte[]> bytes = customer .map(Customer::getAddress) .map(Address::street) .map((String s) -> s.substring(0, 3)) .map(String::toLowerCase) .map(String::getBytes);

Promise

CompletableFuture

Observable

Promise<Customer>

Customer

FOptional

FList

customer.map(Customer::getAddress)

Promise<Address>

map

customer.map()

customer

map()

Promise

From functors to monads

map()

String

FOptional<Integer> tryParse(String s) { try { final int i = Integer.parseInt(s); return FOptional.of(i); } catch (NumberFormatException e) { return FOptional.empty(); } }

tryParse()

String

int

tryParse()

tryParse()

tryParse

String

FOptional

FOptional<String> str = FOptional.of("42"); FOptional<FOptional<Integer>> num = str.map(this::tryParse);

tryParse()

int

FOptional<Integer> num

map()

FOptional<Integer>

FOptional<FOptional<Integer>>

FOptional<Integer> num1 = //... FOptional<FOptional<Integer>> num2 = //... FOptional<Date> date1 = num1.map(t -> new Date(t)); //doesn't compile! FOptional<Date> date2 = num2.map(t -> new Date(t));

FOptional

int

int -> Date

Functor<Integer>

Functor<Date>

num2

num2.map()

int

FOoption<Integer>

java.util.Date

tryParse()

join()

FOptional<Integer> num3 = num2.join()

flatMap()

flatMap()

map

interface Monad<T,M extends Monad<?,?>> extends Functor<T,M> { M flatMap(Function<T,M> f); }

flatMap

flatMap

bind

>>=

FOptional

FOptional<String> num = FOptional.of("42"); FOptional<Integer> answer = num.flatMap(this::tryParse);

map

flatMap()

flatMap

flatMap()

flatMap()

m(x).flatMap(f)

f(x)

x

f

Promise

Promise

Promise

import java.time.DayOfWeek; Promise<Customer> loadCustomer(int id) { //... } Promise<Basket> readBasket(Customer customer) { //... } Promise<BigDecimal> calculateDiscount(Basket basket, DayOfWeek dow) { //... }

Promise<BigDecimal> discount = loadCustomer(42) .flatMap(this::readBasket) .flatMap(b -> calculateDiscount(b, DayOfWeek.FRIDAY));

flatMap()

Promise

loadCustomer()

Promise

readBasket()

Promise

Promise

Exploring flatMap()

import java.time.LocalDate; import java.time.Month; Monad<Month> month = //... Monad<Integer> dayOfMonth = //... Monad<LocalDate> date = month.flatMap((Month m) -> dayOfMonth .map((int d) -> LocalDate.of(2016, m, d)));

Promise

List

Month

Integer

LocalDate

flatMap

map()

Monad<Year>

m

d

liftM2

map

flatMap

Monad<R> liftM2(Monad<T1> t1, Monad<T2> t2, BiFunction<T1, T2, R> fun) { return t1.flatMap((T1 tv1) -> t2.map((T2 tv2) -> fun.apply(tv1, tv2)) ); }

flatMap()

liftM2

liftM2(list1, list2, function)

function

list1

list2

Promise

Promise

join()

flatMap()

filter(Predicate<T>)

map

filter()

FList<Customer> vips = customers.filter(c -> c.totalOrders > 1_000);

From list of monads to monad of list

flatMap()

sequence()

Monad<Iterable<T>> sequence(Iterable<Monad<T>> monads)

loadCustomer(id)

Promise<Customer>

Promise

sequence()

sequence()

concat()

merge()

FList<Promise<Customer>> custPromises = FList .of(1, 2, 3) .map(database::loadCustomer); Promise<FList<Customer>> customers = custPromises.sequence(); customers.map((FList<Customer> c) -> ...);

FList<Integer>

map

FList

database.loadCustomer(id)

Promise

sequence()

sequence()

FList<FOptional<T>>

FOptional<FList<T>>

sequence()

map()

flatMap()

flatMap()

Promise.get()

Promise<T>

T

flatMap()

FOptional.get()

FOptional

FList.get(idx)

for

map()

This article was initially an appendix in our Reactive Programming with RxJava book. However introduction to monads, albeit very much related to reactive programming, didn't suit very well. So I decided to take it out and publish separately as a blog post. I am aware that "my very own, half correct and half complete explanation of monads" is the new "Hello, world" on programming blogs. Yet the article looks at functors and monads from a specific angle of Java data structures and libraries. Thus I thought it's worthwhile sharing.RxJava was designed and built on top of very fundamental concepts like functors , monoids and monads . Even though Rx was modeled initially for imperative C# language and we are learning about RxJava, working on top of similarly imperative language, the library has its roots in functional programming. You should not be surprised after you realize how compact RxJava API is. There are pretty much just a handful of core classes, typically immutable, and everything is composed using mostly pure functions.With a recent rise of functional programming (or functional style), most commonly expressed in modern languages like Scala or Clojure, monads became a widely discussed topic. There is a lot of folklore around them:A monad is a monoid in the category of endofunctors, what's the problem?The curse of the monad is that once you get the epiphany, once you understand - "oh that's what it is" - you lose the ability to explain it to anybody.The vast majority of programmers, especially those without functional programming background, tend to believe monads are some arcane computer science concept, so theoretical that it can not possibly help in their programming career. This negative perspective can be attributed to dozens of articles and blog posts being either too abstract or too narrow. But it turns out that monads are all around us, even is standard Java library, especially since Java Development Kit (JDK) 8 (more on that later). What is absolutely brilliant is that once you understand monads for the first time, suddenly several unrelated classes and abstractions, serving entirely different purposes, become familiar.Monads generalize various seemingly independent concepts so that learning yet another incarnation of monad takes very little time. For example you do not have to learn howworks in Java 8, once you realize it is a monad, you know precisely how it works and what can you expect from its semantics. And then you hear about RxJava which sounds so much different but becauseis a monad, there is not much to add. There are numerous other examples of monads you already came across without knowing that. Therefore this section will be a useful refresher even if you fail to actually use RxJava.Before we explain what a monad is, let's explore simpler construct called a functor . A functor is a typed data structure that encapsulates some value(s). From syntactic perspective a functor is a container with the following API:But mere syntax is not enough to understand what functor is. The only operation that functor provides isthat takes a function. This function receives whatever is inside a box, transforms it and wraps the result as-is into a second functor. Please read that carefully.is always an immutable container, thusnever mutates original object it was executed on. Instead it returns the result (or results - be patient) wrapped in a brand new functor, possibly of different type. Additionally functors should not perform any actions when identity function is applied, that is. Such a pattern should always return either the same functor or an equal instance.Oftenis compared to a box holding instance ofwhere the only way of interacting with this value is by transforming it. However there is no idiomatic way of unwrapping or escaping from the functor. The value(s) always stay within the context of functor. Why are functors useful? They generalize multiple common idioms like collections, promises, optionals, etc. with a single, uniform API that works across all of them. Let me introduce a couple of functors to make you more fluent with this API:An extratype parameter was required to makecompile. What you saw in the preceding example was the simplest functor just holding a value. All you can do with that value is transforming it insidemethod, but there is no way to extract it. This is considered beyond the scope of pure functor. The only way to interact with functor is by applying sequences of type-safe transformations:Or fluently, just like you compose functions:From this perspective mapping over a functor is not much different than just invoking chained functions:Why would you even bother with such verbose wrapping that not only does not provide any added value, but also is not capable of extracting the contents back? Well, it turns out you can model several other concepts using this raw functor abstraction. For examplestarting from Java 8 is a functor withmethod. Let us implement it from scratch:Now it becomes interesting. Anfunctor may hold a value, but just as well it might be empty. It's a type-safe way of encoding. There are two ways of constructing- by supplying a value or creatinginstance. In both cases, just like withis immutable and we can only interact with the value from inside. What differsis that the transformation functionmay not be applied to any value if it is empty. This means functor may not necessarily encapsulate exactly one value of type. It can just as well wrap arbitrary number of values, just like... functor:The API remains the same: you take a functor in a transformation- but the behavior is much different. Now we apply a transformation on each and every item in the, declaratively transforming whole list. So if you have a list ofand you want a list of their streets, it's as simple as:It's no longer as simple as saying, you can't invokeon a collection of customers, you must invokeon each individual customer and then place it back in a collection. By the way Groovy found this pattern so common that it actually has a syntax sugar for that:. This operator, known as spread-dot, is actually ain disguise. Maybe you are wondering why I iterate overmanually insiderather than usings from Java 8:? Does this ring a bell? What if I told youin Java is a functor as well? And by the way, also a monad?Now you should see the first benefits of functors - they abstract away the internal representation and provide consistent, easy to use API over various data structures. As the last example let me introduce promise functor, similar to"promises" that a value will become available one day. It is not yet there, maybe because some background computation was spawned or we are waiting for external event. But it will appear some time in the future. The mechanics of completing aare not interesting, but the functor nature is:Looks familiar? That is the point! The implementation offunctor is beyond the scope of this article and not even important. Enough to say that we are very close to implementingfrom Java 8 and we almost discoveredfrom RxJava. But back to functors.does not hold a value ofjust yet. It promises to have such value in the future. But we can still map over such functor, just like we did withand- the syntax and semantics are exactly the same. The behavior follows what the functor represents. Invokingyieldswhich meansis non-blocking.will not wait for the underlyingpromise to complete. Instead it returns another promise, of different type. When upstream promise completes, downstream promise applies a function passed toand passes the result downstream. Suddenly our functor allows us to pipeline asynchronous computations in a non-blocking manner. But you do not have to understand or learn that - becauseis a functor, it must follow syntax and laws.There are many other great examples of functors, for example representing value or error in a compositional manner. But it is high time to look at monads.I assume you understand how functors work and why are they a useful abstraction. But functors are not that universal as one might expect. What happens if your transformation function (the one passed as an argument to) returns functor instance rather than simple value? Well, functor is just a value as well, so nothing bad happens. Whatever was returned is placed back in a functor so all behaves consistently. However imagine you have this handy method for parsings:Exceptions are side-effects that undermine type system and functional purity. In pure functional languages there is no place for exceptions, after all we never heard about throwing exceptions during math classes, right? Errors and illegal conditions are represented explicitly using values and wrappers. For exampletakes abut does not simply return anor silently throw an exception at runtime. We explicitly tell, through the type system, thatcan fail, there is nothing exceptional or erroneous in having a malformed string. This semi-failure is represented by optional result. Interestingly Java has checked exceptions, the ones that must be declared and handled, so in some sense Java is purer in that regard, it does not hide side-effects. But for better or worse checked exceptions are often discouraged in Java, so let's get back to. It seems useful to composewithalready wrapped inThat should not come as a surprise. Ifwould return anyou would get, but becausefunction returnsitself, it gets wrapped twice into awkward. Please look carefully at the types, you must understand why we got this double wrapper here. Apart from looking horrible, having a functor in functor ruins composition and fluent chaining:Here we try to map over the contents ofby turninginto +Date+. Having a function ofwe can easily transform fromto, we know how it works. But in case ofsituation becomes complicated. Whatreceives as input is no longer anbut anand obviouslydoes not have such a constructor. We broke our functor by double wrapping it. However having a function that returns a functor rather than simple value is so common (like) that we can not simply ignore such requirement. One approach is to introduce a special parameterlessmethod that "flattens" nested functors:It works but because this pattern is so common, special method namedwas introduced.is very similar tobut expects the function received as an argument to return a functor - or monad to be precise:We simply concluded thatis just a syntactic sugar to allow better composition. Butmethod (often calledorfrom Haskell) makes all the difference since it allows complex transformations to be composed in a pure, functional style. Ifwas an instance of monad, parsing suddenly works as expected:Monads do not need to implement, it can be implemented on top ofeasily. As a matter of factis the essential operator that enables a whole new universe of transformations. Obviously just like with functors, syntactic compliance is not enough to call some class a monad, theoperator has to follow monad laws, but they are fairly intuitive like associativity ofand identity. The latter requires thatis the same asfor any monad holding a valueand any function. We are not going to dive too deep into monad theory, instead let's focus on practical implications. Monads shine when their internal structure is not trivial, for examplemonad that will hold a value in the future. Can you guess from the type system howwill behave in the following program? First all methods that can potentially take some time to complete return aWe can now compose these functions as if they were all blocking using monadic operators:This becomes interesting.must preserve monadic type therefor all intermediate objects ares. It is not just about keeping the types in order - preceding program is suddenly fully asynchronous!returns aso it does not block.takes whatever thehas (will have) and applies a function returning anotherand so on and so forth. Basically we built an asynchronous pipeline of computation where the completion of one step in background automatically triggers next step.It is very common to have two monads and combining the value they enclose together. However both functors and monads do not allow direct access to their internals, which would be impure. Instead we must carefully apply transformation without escaping the monad. Imagine you have two monads and you want to combine them:Please take your time to study the preceding pseudo-code. I don't use any real monad implementation likeorto emphasize the core concept. We have two independent monads, one of typeand the other of type. In order to buildout of them we must build a nested transformation that has access to the internals of both monads. Work through the types, especially making sure you understand why we usein one place andin the other. Think how you would structure this code if you had a thirdas well. This pattern of applying a function of two arguments (andin our case) is so common that in Haskell there is special helper function calledthat does exactly this transformation, implemented on top ofand. In Java pseudo-syntax it would look somewhat like this:You don't have to implement this method for every monad,is enough, moreover it works consistently for all monads.is extremely useful when you consider how it can be used with various monads. For examplewill applyon every possible pair of items fromand(Cartesian product). On the other hand for optionals it will apply a function only when both optionals are non-empty. Even better, formonad a function will be executed asynchronously when boths are completed. This means we just invented a simple synchronization mechanism (in fork-join algorithms) of two asynchronous steps.Another useful operator that we can easily build on top ofiswhich takes whatever is inside a monad and discards it entirely if it does not meet certain predicate. In a way it is similar tobut rather than 1-to-1 mapping we have 1-to-0-or-1. Againhas the same semantics for every monad but quite amazing functionality depending on which monad we actually use. Obviously it allows filtering out certain elements from a list:But it works just as well e.g. for optionals. In that case we can transform non-empty optional into an empty one if the contents of optional does not meet some criteria. Empty optionals are left intact.Another useful operator that originates fromis. You can easily guess what it does simply by looking at type signature:Often we have a bunch of monads of the same type and we want to have a single monad of a list of that type. This might sounds abstract to you, but it is impressively useful. Imagine you wanted to load a few customers from the database concurrently by ID so you usedmethod several times for different IDs, each invocation returning. Now you have a list ofs but what you really want is a list of customers, e.g. to be displayed in the web browser.(in RxJavais calledor, depending on use-case) operator is built just for that:Having anrepresenting customer IDs weover it (do you see how it helps thatis a functor?) by callingfor each ID. This leads to rather inconvenient list ofs.saves the day, but once again this is not just a syntactic sugar. Preceding code is fully non-blocking. For different kinds of monadsstill makes sense, but in a different computational context. For example it can change ainto. And by the way, you can implement(just like) on top ofThis is just the tip of the iceberg when it comes to usefulness ofand monads in general. Despite coming from rather obscure category theory, monads proved to be extremely useful abstraction even in object-oriented programming languages such as Java. Being able to compose functions returning monads is so universally helpful that dozens of unrelated classes follow monadic behavior.Moreover once you encapsulate data inside monad, it is often hard to get it out explicitly. Such operation is not part of the monad behavior and often leads to non-idiomatic code. For exampleoncan technically return, but only by blocking, whereas all operators based onare non-blocking. Another example isthat can fail becausemay be empty. Eventhat peeks particular element from a list sounds awkward because you can replaceloops withquite often.I hope you now understand why monads are so popular these days. Even in object-oriented(-ish) language like Java they are quite useful abstraction.