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

Functional Patterns for Beautiful Apps with Elm

Functional Patterns for Beautiful Apps with Elm

Elm is a functional language that compiles to JavaScript. After several years of getting my mind used to thinking in object oriented terms, I discovered Elm and it was a big breath of fresh air.

But I quickly realised that I had to unlearn a great deal of "best practices" and "design patterns" native to today's most popular programming languages.

This talk explores the topic of thinking in Elm. How to translate the previous knowledge we have acquired when building applications into a purely functional language: How to deal with error conditions, concurrent updates and how to create delightful functional APIs are some of the questions this talk will try to answer.

José Lorenzo Rodríguez

November 11, 2016
Tweet

More Decks by José Lorenzo Rodríguez

Other Decks in Programming

Transcript

  1. WHAT WENT WRONG FOR ME Implementation of the observer pattern

    in Java. http://classicprogrammerpaintings.com
  2. I BECAME SCARED OF PROGRAMMING. Composition over inheritance SOLID SRP

    Covariance Polymorphism Generics Singleton Inheritance? Dependency Injection DIC Service Locator HammerFactoryFactoryFactory Service Locator Interface “You’re doing it wrong” LSP Inversion of Control “That’s not really OOP”
  3. THIS IS NOT FUN ANYMORE. Me, 2014 Despite following all

    the best practices I kept making dumb mistakes but also felt overwhelmed by them
  4. THIS IS NOT FUN ANYMORE. Me, 2014 Despite following all

    the best practices I kept making dumb mistakes but also felt overwhelmed by them
  5. } Like a class hierarchy, kind of? } No idea

    what this is, but seems like it updates a model?
  6. } Like a class hierarchy, kind of? } No idea

    what this is, but seems like it updates a model? }This I can understand
  7. } Like a class hierarchy, kind of? } No idea

    what this is, but seems like it updates a model? }This I can understand Oh, This is pretty cool!
  8. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS

    ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B”
  9. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS

    ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B”
  10. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS

    ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B” f string html
  11. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS CAN DESCRIBE OPERATIONS

    ▸ Functions are declarative, what seems to be an operation, is just data describing how the operation should work ▸ First Pattern: Effects are “Things” (data) ▸ This data can be used (or not) to change the external world
  12. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS

    ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour.
  13. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS

    ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2
  14. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS

    ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 Pass an input to a function and get a function back!
  15. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS

    ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 f3 f2 Pass an input to a function and get a function back!
  16. IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS

    ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 f3 f2 Or pass a function to another and get a value back Pass an input to a function and get a function back!
  17. DEPENDENCY INJECTION, FUNCTIONAL STYLE FIRST PATTERN: FUNCTIONS FOR DEPENDENCY INJECTION

    ▸ If things seem repetitive, just create new functions after injecting some of the arguments ▸ Corollary: The “interesting” argument of any function should always be the last one
  18. DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT

    SIMPLER? These are the kind of things that make functional programming scary
  19. DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT

    SIMPLER? } Only these contain runtime data These are the kind of things that make functional programming scary
  20. DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT

    SIMPLER? }Identify parameters that are unlikely to change at runtime } Only these contain runtime data These are the kind of things that make functional programming scary
  21. DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted

    to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY.
  22. DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted

    to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY.
  23. DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted

    to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY. But what are all those complicated type things?
  24. TYPES, GOTTA LOVE THEM ALL! TYPES ARE UNIVERSES ▸ Types

    describe the universe of things: Universe of number, colours, actions… ▸ You can map from an universe to another using a function ▸ Types are very similar to functions in many aspects. For example they can be composed! ▸ In Elm, types are cheap to create and to compose
  25. TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU

    MODEL THE OUTSIDE WORLD, WITH TYPES
  26. TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU

    MODEL THE OUTSIDE WORLD, WITH TYPES } Types can also describe actions
  27. TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU

    MODEL THE OUTSIDE WORLD, WITH TYPES ▸ The compiler will guide on what to do you whenever you add another case to “Msg”. Time saver! } Types can also describe actions
  28. TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU

    MODEL THE OUTSIDE WORLD, WITH TYPES ▸ The compiler will guide on what to do you whenever you add another case to “Msg”. Time saver! ▸ Since the “cases” in a type are closed for extension, you are forced to use “composition over inheritance”. } Types can also describe actions
  29. TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS

    IN THE REAL WORLD } Only two types of currencies allowed
  30. TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS

    IN THE REAL WORLD } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible
  31. TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS

    IN THE REAL WORLD ▸ Types are great for representing your domain. } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible
  32. TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS

    IN THE REAL WORLD ▸ Types are great for representing your domain. ▸ You can also use Types as documentation. Looking at signatures helps understand what’s possible. } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible
  33. TYPES ARE THINGS IN THE REAL WORLD TYPES CAN ALSO

    REPRESENT STATES http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html This helps us prevent showing a UI with stale or not relevant feedback to the user }
  34. TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF

    THIS EXPRESSION? What if it is empty?
  35. TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF

    THIS EXPRESSION? What if it is empty? What if it is not a string?
  36. TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF

    THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string?
  37. TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF

    THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string?
  38. TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF

    THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string? undefined something something
  39. TYPES, GOTTA LOVE THEM ALL! ERROR HANDLING CAN GET UNWIELDY

    And i’m only dealing with he happy path!
  40. TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO

    REPRESENT ERRORS Now we have a way to tell the good from the bad.
  41. TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO

    REPRESENT ERRORS Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result
  42. TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO

    REPRESENT ERRORS We wrap the success value in Ok Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result
  43. TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO

    REPRESENT ERRORS We wrap the success value in Ok And the error value in Err Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result
  44. TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS

    Most processing flows can be done with this two-tracked “Railway” Ok track Error track
  45. MAKING IMPOSSIBLE THINGS IMPOSSIBLE REMEMBER THIS? What if the money

    is in a different currency than the account? Wouldn’t it be better to make this case impossible from the start?
  46. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. } c can be anything, in our case, USD or EUR
  47. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. } c can be anything, in our case, USD or EUR
  48. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. } c can be anything, in our case, USD or EUR
  49. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. } c can be anything, in our case, USD or EUR
  50. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. } c can be anything, in our case, USD or EUR }
  51. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. The parameter c “infects” other types using it. } c can be anything, in our case, USD or EUR }
  52. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. The parameter c “infects” other types using it. } c can be anything, in our case, USD or EUR }
  53. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. The parameter c “infects” other types using it. If all the c’s are not of the same type, the program will not compile! } c can be anything, in our case, USD or EUR }
  54. MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

    We “parametrised” the type Money. The parameter c “infects” other types using it. If all the c’s are not of the same type, the program will not compile! This is safe to do now } c can be anything, in our case, USD or EUR }
  55. PROVIDING IMPLEMENTATIONS DATA IS DATA, FUNCTIONS ARE BEHAVIOUR ▸ Whereas

    in OOP internals are encapsulated, in Functional programming we often expose them. ▸ Types represent the data, functions operating on types provide the behaviour. ▸ Functions in any module can provide more behaviour to any type of data. List f ?? Date f ?? String -> Date f ??
  56. BUT WHAT ABOUT MONADS, MONOIDS AND FUNCTORS…??? Me, constantly Elm

    made functional programming easy to understand for me. The scary terminology is gone, but the great ideas they express are there