it s all about morphisms it s all about morphisms
play

Its All About Morphisms Its All About Morphisms Uberto Barbini - PowerPoint PPT Presentation

Its All About Morphisms Its All About Morphisms Uberto Barbini @ramtop https://medium.com/@ramtop Ab About m out me OOP OO progr grammer er Agi gile le TDD Funct nctio iona nal P PRo Rogr gramming ing Fina nance nce


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

  2. Ab About m out me OOP OO progr grammer er Agi gile le TDD Funct nctio iona nal P PRo Rogr gramming ing Fina nance nce Kot otli lin Ind ndust stry Blog: https://medium.com/ ramtop @ Twitter: @ ramtop @ramtop #morphisms #morphisms #VoxxedVienna

  3. Map Map of th of this p s prese sentation ntation Mono noid id Catego Ca egory Mona nad Funct nctor or Natural T l Transf nsformation on Yone neda da Applic icative Morphism sms a s all th the w e way ay dow down... @ramtop #morphisms #morphisms #VoxxedVienna

  4. I d I don’t on’t care e about out Mona Monads ds, w why sh y shoul ould I? I? Neither do Neith er do I what I ca care a abou out is t is Precisely to defin to define s sys ystem em beh behaviou aviour @ramtop #morphisms #morphisms #VoxxedVienna

  5. This p This p presentati presentati ntation w ntation w on will on will ll b ll b be a s be a s a succ a succ ccess i ccess i if if most of most of mos mos t of you w t of you w you will you will ll not f ll not f not fall not fall all a all a asleep asleep @ramtop #morphisms #morphisms #VoxxedVienna

  6. This p presentati ntation w on will ll b be a s a succ ccess i if You will consider that Functional Programming is about transformations and preserving properties. Not (only) lambdas and flatmap @ramtop #morphisms #morphisms #VoxxedVienna

  7. Wh What is at is t this is C Cat ateg egory thi ory thingy ngy? Invented in 1940s “with the goal of understanding the processes that preserve mathematical st struc ucture.” “Category Theory is about rel elat ation between things” “General abstract no nonsense nsense” @ramtop #morphisms #morphisms #VoxxedVienna

  8. Once upon a time there was a Category of Stuffed Toys and a Category of Tigers... @ramtop #morphisms #morphisms #VoxxedVienna

  9. A Cat Categ egory is is defin efined ed in in 5 5 step eps: 1) A collection 1) A collection of Objects of Objects @ramtop #morphisms #morphisms #VoxxedVienna

  10. A Cat Categ egory is is defin efined ed in in 5 5 step eps: 2) A c 2) A collection ollection of Arrows of Arrows @ramtop #morphisms #morphisms #VoxxedVienna

  11. A Cat Categ egory is is defin efined ed in in 5 5 step eps: 3) Each A ) Each Arrow rrow works orks on on 2 2 Objects Objects @ramtop #morphisms #morphisms #VoxxedVienna

  12. A Cat Categ egory is is defin efined ed in in 5 5 step eps: 4) Arrows can 4) Arrows can be combined be combined @ramtop #morphisms #morphisms #VoxxedVienna

  13. A Cat Categ egory is is defin efined ed in in 5 5 step eps: 5) Each Object 5) Each Object has an Identity has an Identity (an arro (an arrow po pointing ting t to it o itsel elf) @ramtop #morphisms #morphisms #VoxxedVienna

  14. @ramtop #morphisms #morphisms #VoxxedVienna

  15. A Category Exa A Category Examp mple Tube Map: Objects stations � Arrows travel routes � Each Arrows connect 2 stations Arrow composition is travelling along the line Identity Arrow is staying in the same station @ramtop #morphisms #morphisms #VoxxedVienna

  16. London London Tub ube Category Category @ramtop #morphisms #morphisms #VoxxedVienna

  17. Monoi noid Catego Ca egory Mona nad Funct nctor or Natural T l Transf nsformatio ion Yoned neda Appli lica cativ ive This is pr pres esentation entation is a is a Categor tegory y as as well! ell! @ramtop #morphisms #morphisms #VoxxedVienna

  18. AddItem AddItem Ready Rea dy New ewOr Order Dispatch Dispatch Dispatch ched Cancel Cancel Close Close Return Return Ca Cance ncell lled ed Closed ed Ret Returned ed Eve Event nt Source Ca Source Cate tegor ory https://skillsmatter.com/skillscasts/11486-functional-cqrs @ramtop #morphisms #morphisms #VoxxedVienna

  19. Func Functional Pro ional Program ramming ming is als is also a C o a Cat ateg egory ry Each programming language has a Category: Types pes are the objects and Fun unct ctions are the morphisms. Part rtial fu l 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 ( ⊥* ⊥* ) @ramtop #morphisms #morphisms #VoxxedVienna

  20. Change of mindset! Ob Objec ect O Oriented ented Fu Funct ctional nal Living Bacter ng Bacteria Gea ears and and P Pipes es Op Opaqu aque Trans anspar arent nt Hidden St Hi n State ate Immutabl table Stat State Int nterfaces ces Type C Clas asses @ramtop #morphisms #morphisms #VoxxedVienna

  21. Kotl otlin f n for f or functi unctional p onal program rammi ming ng ● Nullable and not-nullable types ● Type Aliases ● Class extensions ● Tail recursion ● Pattern matching (when) ● Arrow-kt bindinds with coroutines ● Arrow-kt Typeclasses (?) @ramtop #morphisms #morphisms #VoxxedVienna

  22. arrow-kt.io @ramtop #morphisms #morphisms #VoxxedVienna

  23. KEEP-87 TypeClasses @ramtop #morphisms #morphisms #VoxxedVienna

  24. Pur Purity ity and and Imm Immutab utability ility For the Category morphisms to work in programming we need Puri rity and Im Immutabi abili lity. But they are not a goal per se, only a necessity for the main goal: co compo posi sition and transf sform rmation. Ultimately everything is converted in assem assembly bly which is neither pure nor immutable. We need those quality only for expo posed sed code @ramtop #morphisms #morphisms #VoxxedVienna

  25. Monoid Monoid What about the category of morphisms of a category? Are they composable? It’s a Category with a single Object and lots of Morphisms A Ca Categor egory y wi with th only one one Obj Objec ect t is a Mo Monoi noid @ramtop #morphisms #morphisms #VoxxedVienna

  26. @ramtop #morphisms #morphisms #VoxxedVienna

  27. Progr Programming ing wi with th Monoids onoids A type class with two methods combine � monoid append empty � neutral element (x <> y) <> z = x <> (y <> z) -- associativity empty <> x = x -- left identity x <> empty = x -- right identity @ramtop #morphisms #morphisms #VoxxedVienna

  28. Gener neric ics Type Contructors ype Contructors List<A> is just an abstract type to build List<Int> List<String> List<User> etc. @ramtop #morphisms #morphisms #VoxxedVienna

  29. Type Cla e Class ss vs vs Inter nterfa face ce ● Interfaces “unify” different types: Cat and Dogs can be treated as Animals ● Type Classes “group” types with similar behaviour, wi witho hout hi hiding t g thei eir t r type pes: Cats and Dogs can both form couples but cats can mate only with cats and dogs with dogs. You cannot represent that with interfaces. @ramtop #morphisms #morphisms #VoxxedVienna

  30. Typecl Typeclas ass Ins Instanc ances es ● 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 @ramtop #morphisms #morphisms #VoxxedVienna

  31. Enough h talk, , let let’s ’s see ee the he co code! e! Mono noid id TypeCla Class Inst nstances. nces... ...Give u e us the co combi bine e ne extens nsion on funct nctio ion @ramtop #morphisms #morphisms #VoxxedVienna

  32. The f e fut utur ure (?) (?) 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 } @ramtop #morphisms #morphisms #VoxxedVienna

  33. Trans ransform ormers ers a a.k.a. F .a. Functors ors Very Important!! @ramtop #morphisms #morphisms #VoxxedVienna

  34. Functors unctors Functors can map both object ects (types) and morph rphism (functions) between two categories Functors map must preserve the structure and some properties But can also work inside the same category (End Endofu funct nctors rs) @ramtop #morphisms #morphisms #VoxxedVienna

  35. Functor Laws unctor Laws Functor 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 @ramtop #morphisms #morphisms #VoxxedVienna

  36. @ramtop #morphisms #morphisms #VoxxedVienna

  37. Try Functor ry Functor Kee eep a a cont ntex ext and M nd Map o oper eratio ion n on `it n `it Fail ilure w without E Excep ceptio ion @ramtop #morphisms #morphisms #VoxxedVienna

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend