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.

6f6dc1b13fd3fe35d36db3adafcb0c8e?s=128

Michał Płachta

May 17, 2017
Tweet

Transcript

  1. 3.
  2. 5.
  3. 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
  4. 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
  5. 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
  6. 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
  7. 13.
  8. 14.
  9. 15.
  10. 16.
  11. 20.
  12. 23.

    @miciek Citizen Future Future List <Citizen> Future List <Citizen> Future

    map flatMap Citizen Future map Integer Future ???
  13. 24.
  14. 27.
  15. 33.

    @miciek Problem: treating 0 as “no value yet” Solution: explicit

    return type Option<T> can be Some<T> when value is present can be None when value is not present We should use types to model our assumptions!
  16. 34.
  17. 40.

    @miciek Problem: not handling Future failures Solution: explicit return type

    (again!) Try<T> can be Success<T> when computation succeeded can be Failure when computation failed We should use types to model our assumptions!
  18. 43.
  19. 48.

    @miciek Problem: cryptic, complicated types Solution: Algebraic Data Types Sum<T>

    can be Something or can be SomethingDifferent We should use types to model our assumptions! can be SomethingElse or Product<T> consists of Something and SomethingElse
  20. 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
  21. 50.
  22. 51.
  23. 54.
  24. 55.
  25. 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
  26. 62.
  27. 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!
  28. 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
  29. 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
  30. 68.
  31. 69.
  32. 71.
  33. 72.

    @miciek Executing a List? CensorFilter Original Tweet Censored Tweet (tweet,filter)

    -> newTweet CensorFilter CensorFilter (tweet,filter) -> newTweet (tweet,filter) -> newTweet (tweet, filter) -> newTweet
  34. 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
  35. 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
  36. 77.
  37. 84.
  38. 85.
  39. 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
  40. 95.
  41. 98.

    @miciek Type class pattern RemoteData RemoteData as JSON Response RemoteData

    JSON Response HTTP Server HTTP Server RemoteData Tweet type class type class members
  42. 102.
  43. 103.

    @miciek Decoupled concerns RemoteData RemoteData as JSON Response RemoteData JSON

    Response HTTP Server HTTP Server RemoteData Tweet type class type class members
  44. 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