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

4 Rules of Simple Design in Functional Programming

4 Rules of Simple Design in Functional Programming

Let's see how Category Theory helps us in writing good functional code!

A practical example of how to design a small application using functional programming principles and respecting Kent Beck's 4 rules.
Functional Programming goals are the same than Object-Oriented Programming
You don’t need a special language or library
You need to study and practice a different paradigm

Uberto Barbini

March 20, 2019
Tweet

More Decks by Uberto Barbini

Other Decks in Programming

Transcript

  1. The 4 Rules
    of Simple Design
    and Category Theory
    The 4 Rules
    of Simple Design
    and Category Theory
    Uberto
    Barbini
    @ramtop

    View Slide

  2. “What is the relation between
    Category Theory
    and Code Quality?
    They seem two completely
    different things to me”
    Marcus Biel @MarcusBiel

    View Slide

  3. Object-Oriented
    Functional
    TL;DR

    View Slide

  4. Code Quality and Categories:
    Category Theory helps us
    To improve our functional design

    View Slide

  5. Code Quality and Categories
    Why Functional Programming?
    How Categories can help?

    View Slide

  6. The Alternative

    View Slide

  7. Kent Beck’s
    Four Rules of Simple Design:
    Passes the tests
    (It works)
    Reveals intention
    (Easy to read and understand)
    No duplication
    (DRY: Don’t Repeat Yourself)
    Fewest elements
    (remove anything that doesn't
    serve the three previous rules)

    View Slide

  8. Functional Programming is a
    programming paradigm that treats
    computation as the evaluation of
    mathematical functions and avoids
    changing-state and mutable data.
    Wikipedia

    View Slide

  9. Alan Kay John McCarthy
    Smalltalk Lisp
    Conversations Transformations

    View Slide

  10. A Story of Two Books

    View Slide

  11. Category Tinted Glasses

    View Slide

  12. The proof of the pudding is
    in the eating

    View Slide

  13. The proof of the Functional
    Programmer is in the coding

    View Slide

  14. What we need is:
    Higher Order Functions
    Immutable Data
    Lazyness

    View Slide

  15. But at the end…
    it’s all about morphisms
    “If I haven’t convinced you yet that category
    theory is all about morphisms then I haven’t
    done my job properly.”
    Bartosz Milewski
    https://bartoszmilewski.com/2015/11/17/its-all-about-morphisms/

    View Slide

  16. Morphism examples in code
    Date → String
    User → Date
    String → Int
    (User → Date) → (User → String)

    View Slide

  17. Morphism examples in code
    Date → String
    dateFormat()
    User → Date
    getBirthday()
    String → Int
    length()
    (User → Date) → (User → String)

    View Slide

  18. What is a Category

    View Slide

  19. London Tube Category

    View Slide

  20. Event Source Category
    for pizza delivery service
    NewOrder
    Ready
    Cancelled
    Returned
    Dispatched
    Closed
    https://skillsmatter.com/skillscasts/11486-functional-cqrs
    AddItem
    Cancel
    Dispatch
    Return
    Close

    View Slide

  21. Birthday Greetings Kata
    Problem: write a program that loads a set of employee records from
    a flat file and then sends a greetings email to all employees whose
    birthday is today. The flat file is a sequence of records, separated by
    newlines; this are the first few lines:
    Doe, John, 1982/10/08, [email protected]
    Ann, Mary, 1975/09/11, [email protected]
    http://matteo.vaccari.name/blog/archives/154

    View Slide

  22. Hexagonal OO Design
    Problem: write a program that loads a set of employee records from
    a flat file and then sends a greetings email to all employees whose
    birthday is today. The flat file is a sequence of records, separated by
    newlines; this are the first few lines:

    View Slide

  23. Categories Functional Design
    Our weapons:
    Immutable Types
    Pure Functions
    Laziness
    No Exceptions

    View Slide

  24. Define Arrows From the Outside
    Filename → Emails sent

    View Slide

  25. Define Arrows From the Outside
    Filename → Emails sent
    Filename → Text → EmailData → Emails sent

    View Slide

  26. Define Arrows From the Outside
    Filename → Emails sent
    Filename → Text → EmailData → Emails sent
    … → Text → CSVrows → Employee → EmailData → …

    View Slide

  27. Define Arrows From the Outside
    Filename → Emails sent
    Filename → Text → EmailData → Emails sent
    … → Text → CSVrows → Employee → EmailData → …
    … → CSVrows → Employee → isTodayBirthday → …

    View Slide

  28. Filename
    TextFile
    EmailData
    Emails sent
    CSVrow
    Employee
    TodayBirthday
    GreetingTemplate
    Today

    View Slide

  29. data class Employee(val firstName: String,
    val lastName: String,
    val dateOfBirth: LocalDate,
    val email: EmailAddress)
    data class Email (val recipient: EmailAddress,
    val subject: String,
    val text: String)
    inline class EmailAddress(val raw: String)
    inline class CsvRow(val raw: String)
    Objects → Types

    View Slide

  30. typealias EmployeeToEmail = (Employee) -> Email
    class EmailTemplate(val msgTemplate: String): EmployeeToEmail {
    override fun invoke(e: Employee): Email =
    Email(e.email, "Greetings",
    msgTemplate.replace("%", e.firstName))
    }
    fun rowToEmployee(csv: CsvRow): Employee =
    csv.raw.split(",").let{
    Employee(
    firstName = it[1].trim(),
    lastName = it[0].trim(),
    email = EmailAddress(it[3].trim()),
    dateOfBirth = LocalDate.parse(it[2].trim(), LOCAL_DATE))
    }
    Morphisms → Pure Functions

    View Slide

  31. Filename
    TextFile
    EmailData
    Emails sent
    CSVrow
    Employee
    TodayBirthday
    GreetingTemplate
    Today
    What about the rest?
    IO, collection, time, errors...

    View Slide

  32. Filename
    TextFile
    EmailData
    Emails sent
    CSVrow
    Employee
    TodayBirthday
    GreetingTemplate
    Today
    Categories
    What about the rest?
    IO, collection, time, errors...

    View Slide

  33. But, but…
    how can we switch category?

    View Slide

  34. View Slide

  35. Simplest functor in our code:
    string.length()

    View Slide

  36. Generics are type builders
    List is not a type but you can build types
    List
    List
    List
    Etc.

    View Slide

  37. Generics are type builders
    List is not a type but you can build types
    List
    List
    List
    Etc.
    Employee → List
    is a Functor to the Category of List
    List is said to have a Functor instance

    View Slide

  38. Employee → List
    is a Functor to the Category of List
    List is said to have a Functor instance
    map is how you translate functions (morphisms)
    val employees: List = …
    val names: List =
    employees.map { emp → emp.name }
    List.map(f: (T) -> R): List

    View Slide

  39. Functors are like Fork lifts, they lift a
    function from
    Employee → String
    to
    List → List
    val employees: List = …
    val names: List =
    employees.map { emp → emp.name }
    List.map(f: (T) -> R): List

    View Slide

  40. Functor for Birthday Filter

    View Slide

  41. Functor for Errors

    View Slide

  42. Functor to Read from IO

    View Slide

  43. The Result

    View Slide

  44. CHECK!
    Passes the tests
    Reveals intention
    No duplication
    Fewest elements

    View Slide

  45. Takeouts
    Functional Programming goals are the same than
    Object-Oriented Programming
    You don’t need a special language or library
    You need to study and practice a different paradigm
    github.com/uberto/birthdaykata

    View Slide

  46. Uberto Barbini
    Blog (jvm performance, kotlin, functional programming):
    medium.com/@ramtop
    Twitter:
    @ramtop
    Programmer OOP
    TDD
    Kotlin
    Agile
    Functional
    Programming
    Finance
    Industry

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. View Slide