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

Let's talk about PureScript

Let's talk about PureScript

Presentation at Frontend Meetup Riga, August 2016.

Kārlis Lauva

August 17, 2016
Tweet

More Decks by Kārlis Lauva

Other Decks in Programming

Transcript

  1. an exciting trip
    through history
    prelude

    View Slide

  2. 1936
    Lambda Calculus is
    born

    View Slide

  3. 1990
    A bunch of smart
    people start working
    Haskell

    View Slide

  4. 1995
    Brendan Eich is hired
    by NetScape to write
    Scheme for the
    browser

    View Slide

  5. Spoiler alert:
    He doesn't write a
    Scheme for NetScape

    View Slide

  6. 2016

    View Slide

  7. You shouldn't be
    sad.

    View Slide

  8. Maybe those
    functional dudes got
    something right.

    View Slide

  9. let's talk about
    purescript
    Kārlis Lauva
    @skazhy
    frontend rīga meetup, 2016

    View Slide

  10. Hello.
    I write Clojure and
    dabble with other
    things
    This is not a monad
    tutorial
    This is, like, my
    opinion

    View Slide

  11. So, Haskell for
    the browser, eh

    View Slide

  12. module Main where
    import Prelude
    import Control.Monad.Eff
    import Control.Monad.Eff.Console
    sup :: String -> String
    sup g = "sup, " ++ g
    main :: forall e. Eff (console :: CONSOLE | e) Unit
    main = do
    log $ sup "world"

    View Slide

  13. View Slide

  14. (function(exports) {
    "use strict";
    var Prelude = PS["Prelude"];
    var Control_Monad_Eff = PS["Control.Monad.Eff"];
    var Control_Monad_Eff_Console = PS["Control.Monad.Eff.Console"];
    var sup = function (g) { return "sup, " + g; };
    var main = Control_Monad_Eff_Console.log(sup("world"));
    exports["main"] = main;
    exports["sup"] = sup;;
    })(PS["Main"] = PS["Main"] || {});
    PS["Main"].main();

    View Slide

  15. Y

    View Slide

  16. turns out, functional
    programming languages
    are really useful for
    web things

    View Slide

  17. but javascript too
    can be purely
    functional

    View Slide

  18. View Slide

  19. let's
    discuss the
    bad parts

    View Slide

  20. [Object object]

    View Slide

  21. an *actual*
    type system

    View Slide

  22. purescript makes
    this example boring

    View Slide

  23. like, really boring

    View Slide

  24. sketchy
    branching

    View Slide

  25. View Slide

  26. View Slide

  27. pattern
    matching

    View Slide

  28. data Behavior = KillHumans | BeNiceToHumans
    type Robot = { naughty :: Boolean }
    interactWithHumans :: Robot -> Behavior
    interactWithHumans { naughty: true } = KillHumans
    interactWithHumans { naughty: false } = BeNiceToHumans

    View Slide

  29. data Behavior
    = KillAllHumans
    | BeNiceToHumans
    | KillOnlyVapers
    type Robot = { niceness :: Int }
    interactWithHumans :: Robot -> Behavior
    interactWithHumans { niceness: 0 } = KillAllHumans
    interactWithHumans { niceness: 1 } = KillOnlyVapers
    interactWithHumans _ = BeNiceToHumans

    View Slide

  30. callback hell

    View Slide

  31. monads

    View Slide

  32. fetchRobots(function(response) {
    assembleFleet(function(robots) {
    if (robots)
    return robots;
    }, function(error { .. });
    }, function(error) {... });

    View Slide

  33. data RobotArmy = Army (List Robot)
    fetchRobots = do
    robotParts <- get "/robotParts"
    robots <- assembleFleet robotParts
    return $ Army robots

    View Slide

  34. View Slide

  35. does purescript
    survive
    in the real
    world?

    View Slide

  36. yes.

    View Slide

  37. View Slide

  38. ecosystem
    and tooling

    View Slide

  39. psc
    short for "purescript compiler"

    View Slide

  40. modules
    managed via bower

    View Slide

  41. • build tool => pulp
    • hoogle for purescript => pursuit

    View Slide

  42. purescript
    -
    javascript
    teamwork

    View Slide

  43. back to killer
    robots

    View Slide

  44. data Behavior = KillHumans | BeNiceToHumans
    instance showBehavior :: Show Behavior where
    show KillHumans = "kill all humans11"
    show BeNiceToHumans = "humans, you are pretty tite"
    type Robot = { naughty :: Boolean }
    interactWithHumans :: Robot -> Behavior
    interactWithHumans { naughty: true } = KillHumans
    interactWithHumans { naughty: false } = BeNiceToHumans
    safeInteract :: Boolean -> String
    safeInteract x =
    show $ interactWithHumans { crazyMurderer : x }

    View Slide

  45. PS.Main.safeInteract(isCrazyMurderingRobot)();

    View Slide

  46. but will
    purescript
    react?

    View Slide

  47. yes.

    View Slide

  48. ok, so what
    have we
    learned

    View Slide

  49. so should we all
    switch to
    purescript today?

    View Slide

  50. not _all_ of us

    View Slide

  51. most of the internet
    runs on vanilla
    javascript just fine
    (for years)

    View Slide

  52. View Slide

  53. View Slide

  54. go check it out
    hack cool things

    View Slide

  55. • purescript.org
    • karlis.me

    View Slide

  56. thanks

    View Slide