Presentation at Frontend Meetup Riga, August 2016.
an exciting tripthrough historyprelude
View Slide
1936Lambda Calculus isborn
1990A bunch of smartpeople start workingHaskell
1995Brendan Eich is hiredby NetScape to writeScheme for thebrowser
Spoiler alert:He doesn't write aScheme for NetScape
2016
You shouldn't besad.
Maybe thosefunctional dudes gotsomething right.
let's talk aboutpurescriptKārlis Lauva@skazhyfrontend rīga meetup, 2016
Hello.I write Clojure anddabble with otherthingsThis is not a monadtutorialThis is, like, myopinion
So, Haskell forthe browser, eh
module Main whereimport Preludeimport Control.Monad.Effimport Control.Monad.Eff.Consolesup :: String -> Stringsup g = "sup, " ++ gmain :: forall e. Eff (console :: CONSOLE | e) Unitmain = dolog $ sup "world"
(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();
Y
turns out, functionalprogramming languagesare really useful forweb things
but javascript toocan be purelyfunctional
let'sdiscuss thebad parts
[Object object]
an *actual*type system
purescript makesthis example boring
like, really boring
sketchybranching
patternmatching
data Behavior = KillHumans | BeNiceToHumanstype Robot = { naughty :: Boolean }interactWithHumans :: Robot -> BehaviorinteractWithHumans { naughty: true } = KillHumansinteractWithHumans { naughty: false } = BeNiceToHumans
data Behavior= KillAllHumans| BeNiceToHumans| KillOnlyVaperstype Robot = { niceness :: Int }interactWithHumans :: Robot -> BehaviorinteractWithHumans { niceness: 0 } = KillAllHumansinteractWithHumans { niceness: 1 } = KillOnlyVapersinteractWithHumans _ = BeNiceToHumans
callback hell
monads
fetchRobots(function(response) {assembleFleet(function(robots) {if (robots)return robots;}, function(error { .. });}, function(error) {... });
data RobotArmy = Army (List Robot)fetchRobots = dorobotParts <- get "/robotParts"robots <- assembleFleet robotPartsreturn $ Army robots
does purescriptsurvivein the realworld?
yes.
ecosystemand tooling
pscshort for "purescript compiler"
modulesmanaged via bower
• build tool => pulp• hoogle for purescript => pursuit
purescript-javascriptteamwork
back to killerrobots
data Behavior = KillHumans | BeNiceToHumansinstance showBehavior :: Show Behavior whereshow KillHumans = "kill all humans11"show BeNiceToHumans = "humans, you are pretty tite"type Robot = { naughty :: Boolean }interactWithHumans :: Robot -> BehaviorinteractWithHumans { naughty: true } = KillHumansinteractWithHumans { naughty: false } = BeNiceToHumanssafeInteract :: Boolean -> StringsafeInteract x =show $ interactWithHumans { crazyMurderer : x }
PS.Main.safeInteract(isCrazyMurderingRobot)();
but willpurescriptreact?
ok, so whathave welearned
so should we allswitch topurescript today?
not _all_ of us
most of the internetruns on vanillajavascript just fine(for years)
go check it outhack cool things
• purescript.org• karlis.me
thanks