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

Pragmatist’s Guide to Functional Geekery

Pragmatist’s Guide to Functional Geekery

Basic functional concepts like immutable data, second-order functions, lambdas and function composition can already be found in the modern programmer’s toolkit.

During this talk you will learn about more advanced functional concepts and how they can solve real problems. I will talk about pattern matching, algebraic data types, functional abstractions, folding and property-based tests.

I will show you a practical example written using today’s Java functional constructs and build up from there. I will use only Java & Vavr, which can improve the code, make it more maintainable and testable.

Michał Płachta

May 17, 2017
Tweet

More Decks by Michał Płachta

Other Decks in Programming

Transcript

  1. @miciek
    Michał Płachta
    Pragmatist`s Guide to
    www.michalplachta.com
    Functional Geekery

    View Slide

  2. @miciek
    Let’s talk functional!

    View Slide

  3. @miciek

    View Slide

  4. @miciek
    Galactic Twitter,
    one week.

    View Slide

  5. @miciek

    View Slide

  6. @miciek
    Being functional...
    higher order functions
    immutable collections
    no reassignments
    functions
    immutable types

    View Slide

  7. @miciek
    Being functional...
    higher order functions
    -style
    immutable collections
    Guava Immutable Collections
    Java 8 Streams
    no reassignments
    Java final keyword
    functions
    Java 8 lambdas
    immutable types
    Lombok @Value

    View Slide

  8. @miciek
    Being functional...
    higher order functions
    -style
    built-in
    and more!
    immutable collections
    built-in
    no reassignments
    val keyword
    functions
    first class citizens
    immutable types
    built-in

    View Slide

  9. @miciek
    Being functional...
    higher order functions
    -style
    built-in
    immutable collections
    built-in
    no reassignments
    val keyword
    functions
    first class citizens
    immutable types
    built-in

    View Slide

  10. @miciek
    Bringing style to

    View Slide

  11. @miciek
    Being functional...
    higher order functions
    -style
    built-in
    and more!
    immutable collections
    built-in
    no reassignments
    Java final keyword
    functions
    built-in
    immutable types
    Lombok @Value

    View Slide

  12. @miciek
    Galactic Twitter,
    one week.

    View Slide

  13. @miciek

    View Slide

  14. @miciek

    View Slide

  15. @miciek

    View Slide

  16. @miciek

    View Slide

  17. @miciek
    Future

    View Slide

  18. @miciek
    Future
    finishes successfully with a result of type T
    fails with an exception

    View Slide

  19. @miciek
    @miciek

    View Slide

  20. @miciek

    View Slide

  21. @miciek
    Future.get
    is a blocking call!

    View Slide

  22. @miciek
    Citizen
    Future Future
    List

    Future
    map ???
    DBClient::getFollowers

    View Slide

  23. @miciek
    Citizen
    Future Future
    List

    Future
    List

    Future
    map
    flatMap
    Citizen
    Future
    map
    Integer
    Future
    ???

    View Slide

  24. @miciek

    View Slide

  25. @miciek
    Future.forEach
    doesn’t block! :)

    View Slide

  26. @miciek
    io.vavr.collection.HashMap
    Option
    can be Some when value is present
    can be None when value is not present

    View Slide

  27. @miciek

    View Slide

  28. @miciek
    polling every 200ms

    View Slide

  29. @miciek
    @miciek

    View Slide

  30. @miciek
    @miciek
    I have 0 followers.
    It’s not possible.

    View Slide

  31. @miciek
    @miciek

    View Slide

  32. @miciek
    Problem: treating 0 as “no value yet”

    View Slide

  33. @miciek
    Problem: treating 0 as “no value yet”
    Solution: explicit return type
    Option
    can be Some
    when value is present
    can be None
    when value is not present
    We should use types to
    model our assumptions!

    View Slide

  34. @miciek

    View Slide

  35. @miciek
    polling every 200ms
    when None is returned
    when Some(102) is returned

    View Slide

  36. @miciek
    @miciek

    View Slide

  37. @miciek
    @miciek
    I can’t check how many followers
    does Obi-wan Kenobi have!

    View Slide

  38. @miciek
    @miciek

    View Slide

  39. @miciek
    Problem: not handling Future failures
    java.lang.Exception: Bad Request:
    citizen with name Obi-wan Kenobi couldn't be found

    View Slide

  40. @miciek
    Problem: not handling Future failures
    Solution: explicit return type (again!)
    Try
    can be Success
    when computation succeeded
    can be Failure
    when computation failed
    We should use types to
    model our assumptions!

    View Slide

  41. @miciek
    io.vavr.concurrent.Future

    View Slide

  42. @miciek
    Pipelining potential failures

    View Slide

  43. @miciek
    polling every 200ms
    when None is returned
    when Some(Success(102))
    is returned
    when Some(Failure) is returned

    View Slide

  44. @miciek
    @miciek

    View Slide

  45. @miciek
    @miciek
    I am not happy with the return
    types you use in the
    application!

    View Slide

  46. @miciek
    @miciek

    View Slide

  47. @miciek
    @miciek
    Future>>>
    I am not happy with the return
    types we use in the application!

    View Slide

  48. @miciek
    Problem: cryptic, complicated types
    Solution: Algebraic Data Types
    Sum
    can be Something or
    can be SomethingDifferent
    We should use types to
    model our assumptions!
    can be SomethingElse or
    Product
    consists of Something and
    SomethingElse

    View Slide

  49. @miciek
    Citizen
    can be Civilian or
    can be Stormtropper
    can be Rebel or
    Stormtrooper
    consists of String name and
    boolean isCloned
    can be Jedi or
    can be Sith or
    ADTs

    View Slide

  50. @miciek

    View Slide

  51. @miciek

    View Slide

  52. @miciek
    How to convert Try into
    RemoteFollowersData?

    View Slide

  53. @miciek
    Pattern Matching

    View Slide

  54. @miciek

    View Slide

  55. @miciek

    View Slide

  56. @miciek
    @miciek

    View Slide

  57. @miciek
    @miciek
    Darth Vader has way too many
    followers! They are all clones!

    View Slide

  58. @miciek
    Clones should not count
    as followers!

    View Slide

  59. @miciek
    Let’s count followers!
    io.vavr.collections.Traversable

    View Slide

  60. @miciek
    Citizen
    any Civilian or
    any Stormtropper that is not cloned
    any Rebel or
    any Jedi or
    any Sith or
    Counting followers
    returns true if

    View Slide

  61. @miciek
    Pattern Matching to the rescue!

    View Slide

  62. @miciek

    View Slide

  63. @miciek
    @miciek

    View Slide

  64. @miciek
    @miciek
    I am not happy! Galactic Twitter
    is used for political purposes!

    View Slide

  65. @miciek
    Fear is the path
    to the Dark Side.
    Yoda
    Help me Obi-Wan
    Kenobi, you're my
    only hope!
    Princess Leia
    Use the Force,
    Luke.
    Obi-Wan Kenobi
    Very political!
    Not cool!

    View Slide

  66. @miciek
    Don’t delete,
    manipulate!
    Censorship module
    ● Condition: If the tweet is pro Dark Side
    ○ Manipulation: Add more Dark Side
    ● If the tweet is pro Light Side
    ○ Replace Force with Dark Side
    ● If the tweet is a general wisdom
    ○ Replace Force with Dark Side
    ○ Add more Dark Side
    ● If the tweet is pro Rebellion and is not a joke
    ○ Hail the Empire
    ● If the tweet is pro Empire and is a joke
    ○ Trash the Rebellion
    ● If the tweet is not pro Empire nor Rebellion
    ○ Add Force and even more Force
    ○ Make joke about it
    ● If the tweet is pro empire
    ○ Add more Dark Side
    ● Always
    ○ Sacrifice for the Empire

    View Slide

  67. @miciek
    No more than 2
    manipulations!
    Censorship module initial filters
    ● If the tweet is pro Dark Side
    ○ Add more Dark Side
    ● If the tweet is pro Light Side
    ○ Replace Force with Dark Side
    ● If the tweet is a general wisdom
    ○ Replace Force with Dark Side
    ○ Add more Dark Side
    ● If the tweet is pro Rebellion and is not a joke
    ○ Hail the Empire
    ● If the tweet is pro Empire and is a joke
    ○ Trash the Rebellion
    ● If the tweet is not pro Empire nor Rebellion
    ○ Add Force and even more Force
    ○ Make joke about it
    ● If the tweet is pro empire
    ○ Add more Dark Side
    ● Always
    ○ Sacrifice for the Empire

    View Slide

  68. @miciek

    View Slide

  69. @miciek

    View Slide

  70. @miciek
    or

    View Slide

  71. @miciek

    View Slide

  72. @miciek
    Executing a List?
    CensorFilter
    Original
    Tweet
    Censored
    Tweet
    (tweet,filter) -> newTweet
    CensorFilter CensorFilter
    (tweet,filter) -> newTweet (tweet,filter) -> newTweet
    (tweet, filter) -> newTweet

    View Slide

  73. @miciek
    Executing a List of ints?
    4
    (0, 4) -> 0 + 4
    6 3
    (4, 6) -> 4 + 6 (10, 3) -> 10 + 3
    Initial = 0
    Output = 13
    (result, x) -> result + x

    View Slide

  74. @miciek
    Executing a List of ints?
    4
    (result, x) -> result + x
    6 3
    (result, x) -> result + x (result, x) -> result + x
    Initial = 0
    io.vavr.collections.List
    Output = 13

    View Slide

  75. @miciek
    CensorFilter
    Original
    Tweet
    Censored
    Tweet
    applyFilter
    CensorFilter CensorFilter
    applyFilter applyFilter

    View Slide

  76. @miciek
    CensorFilter
    CensorStatus
    (Tweet, manipulations)
    CensorStatus
    (censored Tweet, manipulations)
    applyFilter
    CensorFilter CensorFilter
    applyFilter applyFilter

    View Slide

  77. @miciek

    View Slide

  78. @miciek
    Feature Switch!

    View Slide

  79. @miciek
    @miciek

    View Slide

  80. @miciek
    @miciek
    You don’t have any tests!

    View Slide

  81. @miciek
    We used types to model
    our assumptions!

    View Slide

  82. @miciek
    Standard Unit Tests

    View Slide

  83. @miciek
    Problem: writing tests by examples
    Solution: property-based tests
    We should test the
    properties!

    View Slide

  84. @miciek

    View Slide

  85. @miciek

    View Slide

  86. @miciek
    @miciek

    View Slide

  87. @miciek
    @miciek
    Starring
    Coffeehouse Programmer
    Darth Vader
    Luke Skywalker
    Uncle Bob

    View Slide

  88. @miciek
    @miciek
    Directed by
    Future type
    Option type
    Try type
    “monads”

    View Slide

  89. @miciek
    @miciek
    Special Effects
    Algebraic Data Types
    Pattern Matching
    Property-based tests

    View Slide

  90. @miciek
    @miciek
    github.com/miciek/galactic-twitter
    Screenplay
    Java vs Scala
    Web server in Akka HTTP
    Pattern Matching implementation
    RemoteData as a Functor
    Type class pattern
    “Pimp my type” pattern

    View Slide

  91. @miciek
    @miciek
    (Your) Next Episode
    Everything
    as
    Data

    View Slide

  92. @miciek
    Pragmatist`s Guide to
    Functional Geekery
    github.com/miciek/galactic-twitter
    ANY QUESTIONS?
    Michał Płachta
    www.michalplachta.com

    View Slide

  93. @miciek
    @miciek

    View Slide

  94. @miciek
    @miciek
    You don’t have clean code!

    View Slide

  95. @miciek

    View Slide

  96. @miciek
    Concern #1: represent state of db cache Concern #2: represent RemoteData as JSON

    View Slide

  97. @miciek
    Coupled concerns
    RemoteData
    RemoteData as
    JSON Response
    HTTP Server

    View Slide

  98. @miciek
    Type class pattern
    RemoteData
    RemoteData as
    JSON Response
    RemoteData
    JSON Response
    HTTP Server HTTP Server
    RemoteData Tweet
    type class
    type class members

    View Slide

  99. @miciek
    Pure values, no mess!

    View Slide

  100. @miciek
    But the server needs an
    instance of type class!

    View Slide

  101. @miciek
    Compiler will use them
    automatically!

    View Slide

  102. @miciek

    View Slide

  103. @miciek
    Decoupled concerns
    RemoteData
    RemoteData as
    JSON Response
    RemoteData
    JSON Response
    HTTP Server HTTP Server
    RemoteData Tweet
    type class
    type class members

    View Slide

  104. @miciek
    Future type lib lib* built-in
    Option type lib lib* built-in
    Try type lib lib* built-in
    ADTs lib built-in built-in
    Pattern Matching lib kind of* built-in
    Property tests lib lib lib
    * more:
    https://github.com/MarioAriasC/funKTionale
    https://kotlin.link/articles/Kotlin-for-Scala-Developers.html
    https://programmingideaswithjake.wordpress.com/2016/08/27/improved-pattern-matching-in-kotlin/
    Everything as Data

    View Slide