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

PureScript Meetup #10

Fd9f21969cd8a0cbf0b2f48a9af0cc28?s=47 cybai
November 24, 2017

PureScript Meetup #10

PureScript Meetup #10 @ PureScript.tw
PureScript By Example: CH1 - CH4

Official document: https://pursuit.purescript.org/
Playground: http://try.purescript.org/
Why You Should Use PureScript: https://gist.github.com/paf31/adfd15fbb1ac8b99fc68be2c9aca8427
Why the PureScript community uses Bower: http://harry.garrood.me/blog/purescript-why-bower/
ES6 compatible table: https://kangax.github.io/compat-table/es6/
PureScript Tutorial 1 by Ray Shih: https://www.slideshare.net/mnfshih/purescript-tutorial-1
Getting Started with PureScript by Michael Ficarra: https://speakerdeck.com/michaelficarra/getting-started-with-purescript
Update from Bower to Psc-Package: https://qiita.com/kimagure/items/0d9354900d7a7dbd3864

Fd9f21969cd8a0cbf0b2f48a9af0cc28?s=128

cybai

November 24, 2017
Tweet

Transcript

  1. PureScript Meetup #10 PureScript By Example: CH1 - CH4 CYBAI

    _cybai 1
  2. Agenda • What is PureScript? • Environment Setup • Syntax

    • Literals • Records • Function • Types • Kinds • Type Signature 2
  3. Agenda (Cont.) • Syntax • Curried Function, where clause •

    Infix function • Function Composition (<<<, >>>) • Array • map, range, filter, concat, concatMap • do notation • guard • foldl, foldr • Tail Recursion 3
  4. What is PureScript? • A static typed language • A

    pure functional language • Compiles to JavaScript • Inspired by Haskell 4
  5. Why PureScript? Ref: Why You Should Use PureScript 5

  6. Why Pure Functional Language? 6

  7. Why not JS? Drawbacks in JavaScript • Unrestricted • Untyped

    • Not easy to form an abstraction 7
  8. Environment Setup 8

  9. Installation • npm i -g purescript pulp bower1 • pulp

    init • pulp build • pulp run • pulp repl 1: Why the PureScript community uses Bower 9
  10. IDE Plugins 10

  11. Useful websites • Official documents: • https://pursuit.purescript.org/ • Playground: •

    http://try.purescript.org/ 11
  12. Syntax 12

  13. Literals a = 0.0 :: Number b = 0 ::

    Int c = "purescript" :: String d = 'a' :: Char e = true :: Boolean f = [0] :: Array Int 13
  14. Records > meetup = { category: "PureScript", place: "Taiwan" }

    > :type meetup { category :: String , place :: String } 14
  15. Function > meetupPlace m = m.category <> " meetup @"

    <> m.place > m = { category: "PureScript", place: "Taiwan" } > meetupPlace m "PureScript meetup @Taiwan" 15
  16. Types type Meetup = { category :: String , place

    :: String } 16
  17. Kinds > :kind Number Type > import Data.List > :kind

    List Type -> Type > :kind List String Type Type Constructor 17
  18. Type Signature Function Name Function Parameter Types add :: Number

    -> Number -> Number add x y = x + y Return Type 18
  19. Curried Function addMeetup :: Meetup -> Meetups -> Meetups addMeetup

    m a = Cons m a > :type addMeetup meetup List { category :: String , place :: String } -> List { category :: String , place :: String } 19
  20. Curried Function (Cont.) addMeetup :: Meetup -> Meetups -> Meetups

    addMeetup = Cons 20
  21. where clause findMeetup :: String -> String -> Meetups ->

    Maybe Meetup findMeetup category place meetups = head $ filter filterMeetup meetups where filterMeetup :: Meetup -> Boolean filterMeetup m = m.category == category && m.place == place 21
  22. Infix Function apply :: forall a b. (a -> b)

    -> a -> b apply f x = f x infixr 0 apply as $ 22
  23. Infix Function (Cont.) head $ filter filterMeetup meetups head (filter

    filterMeetup meetups) 23
  24. Function Composition • <<< : backwards composition • >>> :

    forwards composition 24
  25. Function Composition (Cont.) findMeetup :: String -> String -> Meetups

    -> Maybe Meetup findMeetup category place = head <<< filter filterMeetup where ... findMeetup :: String -> String -> Meetups -> Maybe Meetup findMeetup category place = filter filterMeetup >>> head where ... 25
  26. Recursion, Maps And Folds 26

  27. Recursion: fib fib :: Int -> Int fib 0 =

    1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) 27
  28. Recursion: array import Prelude import Data.Array (null) import Data.Array.Partial (tail)

    import Partial.Unsafe (unsafePartial) length :: forall a. Array a -> Int length arr = if null arr then 0 else 1 + length (unsafePartial tail arr) 28
  29. map map (\n -> n + 1) [1, 2, 3,

    4, 5] (\n -> n + 1) `map` [1, 2, 3, 4, 5] (\n -> n + 1) <$> [1, 2, 3, 4, 5] 29
  30. map (Cont.) > :type map forall a b f. Functor

    f => (a -> b) -> f a -> f b forall a b. (a -> b) -> Array a -> Array b > show <$> [1, 2, 3, 4, 5] ["1","2","3","4","5"] 30
  31. range infix 8 range as .. > 1 .. 5

    [1, 2, 3, 4, 5] > show <$> (1 .. 5) ["1","2","3","4","5"] 31
  32. filter > filter (\n -> n `mod` 2 == 0)

    (1 .. 10) [2,4,6,8,10] 32
  33. concat > :type concat forall a. Array (Array a) ->

    Array a > concat [[1, 2, 3], [4, 5], [6]] [1, 2, 3, 4, 5, 6] 33
  34. concatMap > :type concatMap forall a b. (a -> Array

    b) -> Array a -> Array b > concatMap (\n -> [n, n * n]) (1 .. 5) [1,1,2,4,3,9,4,16,5,25] 34
  35. Let’s write a factor function. 35

  36. factor (Cont.) > import Data.Array > pairs n = concatMap

    (\i -> 1 .. n) (1 .. n) > pairs 3 [1,2,3,1,2,3,1,2,3] 36
  37. factor (Cont.) > :paste … pairs' n = … concatMap

    (\i -> … map (\j -> [i, j]) (1 .. n) … ) (1 .. n) …^D > pairs' 3 [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]] 37
  38. factor (Cont.) > :paste … pairs'' n = … concatMap

    (\i -> … map (\j -> [i, j]) (i .. n) … ) (1 .. n) …^D > pairs'' 3 [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] 38
  39. factor (Cont.) > import Data.Foldable > factors n = filter

    (\pair -> product pair == n) (pairs'' n) > factors 10 [[1,10],[2,5]] 39
  40. `do` notation > :paste … do … i <- 1

    ..3 … pure i …^D [1,2,3] 40
  41. `do` notation (Cont.) > :paste … do … i <-

    1 ..3 … j <- i ..3 … pure [i,j] …^D [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] 41
  42. `do` notation (Cont.) Let’s refactor the `factor` function with `do`

    notation. factors :: Int -> Array (Array Int) factors n = filter (\xs -> product xs == n) $ do i <- 1 .. n j <- i .. n pure [i, j] `pure` will be introduced in later Chapter; we can just understand it easily as returning the corresponding Type. In this example, we can also write `[[i,j]]`. 42
  43. `do` notation (Cont.) Let’s refactor the `factor` function with `do`

    notation. import Control.MonadZero (guard) factors :: Int -> Array (Array Int) factors n = do i <- 1 .. n j <- i .. n guard $ i * j == n pure [i, j] Those code below `guard` will only be executed when its result is `true` 43
  44. Folds > import Data.Array (foldl, foldr) > :type foldl forall

    a b. (b -> a -> b) -> b -> Array a -> b > :type foldr forall a b. (a -> b -> b) -> b -> Array a -> b 44
  45. Folds (Cont.) > foldl (\acc n -> acc <> show

    n) "" [1,2,3,4,5] "12345" > foldr (\n acc -> acc <> show n) "" [1,2,3,4,5] "54321" 45
  46. Tail Recursion 46

  47. Tail Recursion (Cont.) > f 0 = 0 > f

    n = 1 + f (n - 1) > f 10 10 > f 100000 RangeError: Maximum call stack size exceeded 47
  48. Tail Recursion (Cont.) fib :: Int -> Int fib =

    fib' 0 1 where fib' :: Int -> Int -> Int -> Int fib' acc nx 0 = (acc + nx) fib' acc nx x = fib' (acc + nx) acc (x - 1) 48
  49. Tail Recursion in JS 49 Ref: ES6 compatible table

  50. Q & A 50

  51. References • PureScript Tutorial 1 by Ray Shih • Getting

    Started with PureScript by Michael Ficarra • Why the PureScript community uses Bower • Update from Bower to Psc-Package • Why You Should Use PureScript 51
  52. Thank you! 52