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 full-size slide

  2. @miciek
    Let’s talk functional!

    View full-size slide

  3. @miciek
    Galactic Twitter,
    one week.

    View full-size slide

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

    View full-size slide

  5. @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 full-size slide

  6. @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 full-size slide

  7. @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 full-size slide

  8. @miciek
    Bringing style to

    View full-size slide

  9. @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 full-size slide

  10. @miciek
    Galactic Twitter,
    one week.

    View full-size slide

  11. @miciek
    Future

    View full-size slide

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

    View full-size slide

  13. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  15. @miciek
    Citizen
    Future Future
    List

    Future
    map ???
    DBClient::getFollowers

    View full-size slide

  16. @miciek
    Citizen
    Future Future
    List

    Future
    List

    Future
    map
    flatMap
    Citizen
    Future
    map
    Integer
    Future
    ???

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. @miciek
    polling every 200ms

    View full-size slide

  20. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  22. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  24. @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 full-size slide

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

    View full-size slide

  26. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  28. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  30. @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 full-size slide

  31. @miciek
    io.vavr.concurrent.Future

    View full-size slide

  32. @miciek
    Pipelining potential failures

    View full-size slide

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

    View full-size slide

  34. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  36. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  38. @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 full-size slide

  39. @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 full-size slide

  40. @miciek
    How to convert Try into
    RemoteFollowersData?

    View full-size slide

  41. @miciek
    Pattern Matching

    View full-size slide

  42. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  44. @miciek
    Clones should not count
    as followers!

    View full-size slide

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

    View full-size slide

  46. @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 full-size slide

  47. @miciek
    Pattern Matching to the rescue!

    View full-size slide

  48. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

  50. @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 full-size slide

  51. @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 full-size slide

  52. @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 full-size slide

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

    View full-size slide

  54. @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 full-size slide

  55. @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 full-size slide

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

    View full-size slide

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

    View full-size slide

  58. @miciek
    Feature Switch!

    View full-size slide

  59. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  62. @miciek
    Standard Unit Tests

    View full-size slide

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

    View full-size slide

  64. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  68. @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 full-size slide

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

    View full-size slide

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

    View full-size slide

  71. @miciek
    @miciek

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  76. @miciek
    Pure values, no mess!

    View full-size slide

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

    View full-size slide

  78. @miciek
    Compiler will use them
    automatically!

    View full-size slide

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

    View full-size slide

  80. @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 full-size slide