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 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
  2. @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
  3. @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
  4. @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
  5. @miciek Citizen Future Future List <Citizen> Future List <Citizen> Future

    map flatMap Citizen Future map Integer Future ???
  6. @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!
  7. @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!
  8. @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
  9. @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
  10. @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
  11. @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!
  12. @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
  13. @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
  14. @miciek Executing a List? CensorFilter Original Tweet Censored Tweet (tweet,filter)

    -> newTweet CensorFilter CensorFilter (tweet,filter) -> newTweet (tweet,filter) -> newTweet (tweet, filter) -> newTweet
  15. @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
  16. @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
  17. @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
  18. @miciek Type class pattern RemoteData RemoteData as JSON Response RemoteData

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

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