it’s all about morphisms - jax londoni don’t care about monads, why should i? p r e c i s e l y...

78
It’s All About Morphisms It’s All About Morphisms Uberto Barbini @ramtop https://medium.com/@ramtop

Upload: others

Post on 27-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

It’s All About MorphismsIt’s All About Morphisms

Uberto Barbini @ramtop https://medium.com/@ramtop

Page 2: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

About meprogrammer OOP

TDD

Kotlin

Agile

Functional PRogramming

Blog: https://medium.com/ ramtop@

Twitter: ramtop@

FinanceIndustry

Page 3: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Map of this presentationCategory Monoid

Functor

Applicative

Monad

Natural Transformation

Morphisms all the way down...

Yoneda

Page 4: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

I don’t care about Monads, why should I?

Precisely

Neither do Iwhat I care about is

to define system behaviour

Page 5: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

This presentation will be a success if most of you will not fall asleep

This presentation will be a success if most of you will not fall asleep

Page 6: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

You will consider that Functional Programming is about transformations and

preserving properties.Not (only) lambdas and

flatmap

This presentation will be a success if

Page 7: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

What is this Category thingy?Invented in 1940s “with the goal of understanding the processes that preserve mathematical structure.”

“Category Theory is about relation between things”

“General abstract nonsense”

Page 8: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Once upon a time there was a Category of Stuffed Toys and a

Category of Tigers...

Page 9: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

1) A collection of Objects

A Category is defined in 5 steps:

Page 10: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

2) A collection of Arrows

A Category is defined in 5 steps:

Page 11: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

3) Each Arrow works on 2 Objects

A Category is defined in 5 steps:

Page 12: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

4) Arrows can be combined

A Category is defined in 5 steps:

Page 13: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

5) Each Object has an Identity(an arrow pointing to itself)

A Category is defined in 5 steps:

Page 14: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 15: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

A Category ExampleTube Map: Objects stations→ stations

Arrows travel routes→ stations

Each Arrows connect 2 stationsArrow composition is travelling along the lineIdentity Arrow is staying in the same station

Page 16: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

London Tube Category

Page 17: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

This presentation is a Category as well!

Category Monoid

Functor

Applicative

Monad

Natural TransformationYoneda

Page 18: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Event Source Category

NewOrder Ready

CancelledReturned

Dispatched

Closed

https://skillsmatter.com/skillscasts/11486-functional-cqrs

AddItem

Cancel

Dispatch

ReturnClose

Page 19: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Functional Programmingis also a Category

Each programming language has a Category:

Types are the objects and Functions are the morphisms.

Partial functions don’t have a defined return for all inputs.

In reality all programming functions are partial: they can raise Exceptions or never end.

They always have an hidden return of Bottom Type ( )⊥)

Page 20: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Change of mindset!

Object Oriented FunctionalLiving Bacteria Gears and Pipes

Opaque TransparentHidden State Immutable StateInterfaces Type Classes

Page 21: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Kotlin for functional programming● Nullable and not-nullable types● Type Aliases● Class extensions● Tail recursion● Pattern matching (when)● Arrow-kt bindinds with coroutines● Arrow-kt Typeclasses (?)

Page 22: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

arrow-kt.io

Page 23: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

KEEP-87 TypeClasses

Page 24: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Purity and ImmutabilityFor the Category morphisms to work in programming we need Purity and Immutability.

But they are not a goal per se, only a necessity for the main goal: composition and transformation.

Ultimately everything is converted in assembly which is neither pure nor immutable. We need those quality only for exposed code

Page 25: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

MonoidWhat about the category of morphisms of a category?Are they composable?It’s a Category with a single Object and lots of Morphisms

A Category with only one Object is a Monoid

Page 26: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 27: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Programming with MonoidsA type class with two methods

combine monoid append→ stations

empty neutral element→ stations

(x <> y) <> z = x <> (y <> z) -- associativity

empty <> x = x -- left identity

x <> empty = x -- right identity

Page 28: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Generics Type Contructors

List<A> is just an abstract type to build List<Int> List<String> List<User> etc.

Page 29: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Type Class vs Interface● Interfaces “unify” different types:

Cat and Dogs can be treated as Animals● Type Classes “group” types with similar behaviour, without

hiding their types: Cats and Dogs can both form couples but cats can mate only with cats and dogs with dogs. You cannot represent that with interfaces.

Page 30: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Typeclass Instances ● Typeclasses work with instances (like a singleton)● List is not a Monoid nor a Functor nor a Monad but it

has an (or more) instance of Monoid one of Functor and one of Monad

● Technically we implement instances as an interface with a singletons specific implementation.

● We can have different implementation for difference in evaluation, for example because of concurrency

Page 31: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Enough talk, let’s see the code!

MonoidTypeClass Instances...

...Give us the combine extension function

Page 32: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

The future (?)extension interface Monoid<T> {

infix fun T.add(t: T): T

}

extension object IntMonoid: Monoid<Int> {

inline fun Int.add(t: Int) = this + t

}

inline fun <T> sum(t1: T, t2: T, t3: T, with Monoid<T>) = t1 add t2 add t3

fun main() {

sum(1, 2, 3) //no boxing because of inlining

}

Page 33: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Transformers a.k.a. Functors

Very Important!!

Page 34: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

FunctorsFunctors can map both objects (types) and morphism (functions) between two categories

Functors map must preserve the structure and some properties

But can also work inside the same category (Endofunctors)

Page 35: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Functor LawsFunctor is a TypeClass with a Map function that works like this. Id is the identity function.

map id x = x

map (g <> f) = map g <> map f)

Passing the ID function must return the original value

Map must honour associativity of two functions

Page 36: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 37: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Try Functor

Failure without Exception

Keep a context and Map operation on `it

Page 38: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Functors are also Forklifts

val lifted = Try.functor().lift {x:String -> x.toInt()}

lifted(Try.Success("42"))

//Try.Success(42)

Lift a function from A --> B to F<A> --> F<B>

Page 39: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Yoneda LemmaYoneda's lemma concerns functors from a fixed category C to the category of sets, Set. If C is a locally small category (i.e. the hom-sets are actual sets and not proper classes), then each object A of C gives rise to a natural functor to Set called a hom-functor.

Page 40: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Yoneda LemmaF<?>.map(A→B): F<B> (? must be A)

The actual implementation is useful to compose of mappings without executing until we decide

combine all the maps in one and then apply it.No copies of List

Page 41: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Natural TransformationsA natural transformation provides a way of transforming one functor into another while respecting the internal structure of the categories involved.

Transforming Data → Functions

Transforming Functions → Functors

Transforming Functors → Natural Transformations

Page 42: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Natural Transformations

Page 43: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Natural Transformations val list = Try {"3".toInt()}.toOption().toList()

//[3]

val fail = Try {"xyz".toInt()}.toOption().toList()

//[]

Page 44: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Combining FunctorsWe can imagine 2 ways to combine 2 functors

F + F = F F<f> map F<a> = F<f(a)> or F * F = F flatmap (a → F<a → F<a>>) = F a

Page 45: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Applicative Functors● We can combine a value inside a Functor with a function

inside another Functor● If the function want more than 1 param, it will return a

function with x-1 params. ● Applicative can apply: ● F<A> applied to F<A->B> to create F<B>

Page 46: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Try Applicative Functor

Silly example of function that can raise an Exception

Exception raised!

Page 47: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

The M word...● What about a category of (endo)functors?● Some functors have a monoid instance, others not. ● How can we call the Category of Endofunctors with a

Monoid instance?

A Monad is just a Monoid in the category of Endofunctors, what's the problem?

Page 48: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monad Recipe

Page 49: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monads Allow Sequences of Instructions

Page 50: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monads BindingFrom here:val university: IO<University> = getStudent("John Smith").flatMap { student -> getUniversity(student.universityId).flatMap { university -> getDean(university.deanId) } }

To here:val university: IO<University> = IO.monad().binding { val student = getStudent("John Smith").bind() val university = getUniversity(student.universityId).bind() val dean = getDean(university.deanId).bind() dean }

Page 51: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Functional Programming Dilemma:

Perfectly Pure Programs are Perfectly Useless

Enter Effect

s

Page 52: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Dependency Injectioncourtesy of Reader Monad

What’s happen if user cannot be fetched?

Runs here

fun getUser(userId:String):Reader<Context, User> {fun getCommonFriends(u1: User, u2: User): Reader<Context, List<User>>

Page 53: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monads ZooAll Monads are also Functors and Applicative, but the opposite is not true.

Each Monad as it’s specific logic on top of the Monads Laws

These are already available in many libraries but you can extend and create your own.

● Option

● Try

● List

● Either

● Reader

● Writer

● State

● IO

● Free

Page 54: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Why studying Categories?

Morphisms allows you to work at compile time with your Domain Knowledge

Learn how to compose functions and preserve properties

Monads and other typeclasses should emerge from your code, not the other way round

Learn a common terminology and the reasons behind that

Page 55: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

How to use morphisms in real world programs?

Page 56: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Real World ExamplesWeb Server as a function github.com/http4k/http4k

Functional Domain Design github.com/uberto/anticapizzeria

Page 57: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

To learn more:Category Theory for Programmers bartoszmilewski.com

Cats in scala typelevel.org/catsArrow in Kotlin arrow-kt.io

If you enjoyed please follow me on twitter and medium ramtop@ramtop

Page 58: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 59: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 60: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 61: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Page 62: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Endofunctors● In programming we only work with Endofunctors in the

Category of our types● Endofunctor map a => F a where F a is a type

constructor in the same category of a (the type system category)

● Endofunctors are very important because we can compose them!

Page 63: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Higher Kinded TypesIn Java we cannot abstract at more than one level like List<A<B>> or ignoring the second level List<A<?>>

But in Functional programming makes sense generalise over the container or context like:X<A> + f(A)->B = X<B>

Page 64: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monads Laws

"The diagram commutes" means that the map produced by following any path through the diagram is the same.

Page 65: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

List Applicative

Can you guess how it work?

Transform a standard List in a Arrow List (ListK)

Page 66: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

List Map2

Exactly same result

Page 67: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Reader: A Functor on functions Val userId = 123

val reader = Reader{dbUrl:String -> DbConn(dbUrl)}

val name = reader.run("myDbConn")

.map{it.getUser(userId) }

.map{it.name}

.value()

//"Joe"

Page 68: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Purity Bubbles + Events = Actors

Page 69: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

"Rather than thinking about function purity as a goal, you have to think about which properties you want your program to preserve." @raulraja

Perfectly Pure Programs are Perfectly Useless

Page 70: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

London Tube Map is a Monoid! (if you squint hard enough)

Page 71: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Ok… but why using a Object Oriented Language for Functional Programming at all?

Page 72: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Conclusions:why functional programming?

More composition

Less boilerplate

Less repetitions

Less bugs

Less need for tests

Easier concurrency

Less sugar

Different thinking

Performance issues

Complex state

More precision

Different testing style

VS

Page 73: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Monads Transformers

● Monads don’t compose

Page 74: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Validated example

● Better than Try

Page 75: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Kleisli ArrowsOriginal problem: Monads don’t allow for concurrency

Arrows can be useful in Reactive Functional Programming

Kleisli is a type of Arrow for a Monadic context

It’s old ReaderT actually

Page 76: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Arrows

Page 77: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

CandyDispenser

● Code example● Let’s bring all together● We have a function Seed (Seed, Candy)→ stations

● Another one (State, Input) State→ stations

● How can we combine them?● We want

(Dispenser, [Input]) (Dispenser, [Candy])→ stations

Page 78: It’s All About Morphisms - JAX LondonI don’t care about Monads, why should I? P r e c i s e l y Neither do I what I care about is to define system behaviour #morphisms#VoxxedVienna

#VoxxedVienna#morphisms#morphisms @ramtop

Adjoint FunctorsFunctor F from category D to C

Functor G from C to D

Every adjunction 〈 F, G, , ε, η η 〉 gives rise to an associated monad 〈 T, , η μ 〉 in the category D.