Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FP in Scala 106: Set Category

FP in Scala 106: Set Category

Set category
Function programing aims at build large scale programs based on small composed functions. Those smaller functions are the atomic capabilities of the programs. Once composed with each other, they form a bigger capability, yet testable and still composable.

We've already seen functions compositions in previous a session.

We've seen that some abstractions make possible a higher level for programing for a better and safer code re-usage. Functors and Applicatives and Monads are the most famous ones that we've already studies together.

What is interesting is that all of this is supported by a branch of mathematical study, Cateogry Theory. Another branch study a complementary field, the Sets. Let's have a look together about this today. We'll talk about weird structures such SemiGroup, Monoid, some mathematical properties such as associativity or commutativity.

Those categories will also be implemented with the Type Class pattern.

Xavier Bucchiotty

April 04, 2016
Tweet

More Decks by Xavier Bucchiotty

Other Decks in Technology

Transcript

  1. CATEGORY THEORY C B g : => C f o

    g : => A A B f : =>
  2. SET CATEGORY C B g : => C f o

    g : => A B f : => A
  3. "Being abstract is something profoundly different from being vague …

    The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise." Ew Dijkstra TEXTE
  4. SET A x x x x x x x x

    x x x x x x x x x x
  5. SEMI GROUP A semigroup is a set S together with

    a binary operation "•" that satisfies the associative property: For all a,b,c in S, the equation (a • b) • c = a • (b • c) holds. HTTPS://EN.WIKIPEDIA.ORG/WIKI/SEMIGROUP
  6. SEMI GROUP A x x x x x x x

    x x x x x x x x x x x A A f : => => A
  7. SEMI GROUP f ( a , b ) = d

    val d = f ( a , b )
  8. SEMI GROUP x == y val x = f (

    a , f (b , c) ) val y = f ( f ( a , b ) , c)
  9. SEMI GROUP a b c d c x a b

    c e a f f f f FOLDLEFT FOLDRIGHT
  10. MONOID A monoid is a semigroup with an identity element.

    Identity element There exists an element e in S such that for every element a in S, the equations e • a = a • e = a hold. HTTPS://EN.WIKIPEDIA.ORG/WIKI/MONOID
  11. MONOID A x x x x x x x x

    x x x x x x x x x x A A f : => => A
  12. MONOID A x x x x x x x x

    x x x x x x x x x x A A f : => => A id
  13. MONOID val x = f ( a , id )

    => x == a val y = f ( id , b ) => y == b f ( id , id ) = id
  14. COMMUTATIVITY A binary operation * on a set S is

    called commutative if: x * y = y * x for all x,y in S HTTPS://EN.WIKIPEDIA.ORG/WIKI/COMMUTATIVE_PROPERTY
  15. a b c d COMMUTATIVITY z f Even in parallel,

    order of processing in f is not important
  16. ACID 2.0 * ASSOCIATIVE * COMMUTATIVE * IDEMPOTENT * DISTRIBUTED

    Jonas Bonér HTTP://FR.SLIDESHARE.NET/JBONER/LIFE-BEYOND-THE-ILLUSION-OF-PRESENT COMMUTATIVITY
  17. ADDITIVITY OR MULTIPLICITY BOOLEAN TRUE FALSE f = OR MULTIPLICATIVE

    MONOID ADDITIVE MONOID id = false (zero) f = AND id = true (one)
  18. What Is A Good Language for Design? One that helps

    discovering great designs. Pattern ꔄ Abstractions Constraints ꔄ Types Martin Odersky BLOG.XEBIA.FR/2014/09/17/TROUBLES-WITH-TYPES/ COMMUTATIVITY
  19. LINKS ▸ Martin Odersky, Troubles with Types ▸ ▸ Jonas

    Bonér, Beyond the illusion of present ▸ ▸ Functional Programing in Scala ▸ ▸ Function ▸ BLOG.XEBIA.FR/2014/09/17/TROUBLES-WITH-TYPES/ HTTPS://VIMEO.COM/130759483 HTTPS://WWW.MANNING.COM/BOOKS/FUNCTIONAL-PROGRAMMING-IN-SCALA HTTPS://WWW.MANNING.COM/BOOKS/FUNCTIONAL-AND-REACTIVE-DOMAIN- MODELING
  20. LINKS ▸ Code, powered by Scala && Typelevel Cats ▸

    GITHUB.COM/XEBIA-FRANCE/FP_IN_SCALA_6