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

PureScript Meetup #10

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

cybai

November 24, 2017
Tweet

More Decks by cybai

Other Decks in Programming

Transcript

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

    • Literals • Records • Function • Types • Kinds • Type Signature 2
  2. 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
  3. What is PureScript? • A static typed language • A

    pure functional language • Compiles to JavaScript • Inspired by Haskell 4
  4. Installation • npm i -g purescript pulp bower1 • pulp

    init • pulp build • pulp run • pulp repl 1: Why the PureScript community uses Bower 9
  5. Literals a = 0.0 :: Number b = 0 ::

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

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

    <> m.place > m = { category: "PureScript", place: "Taiwan" } > meetupPlace m "PureScript meetup @Taiwan" 15
  8. Kinds > :kind Number Type > import Data.List > :kind

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

    -> Number -> Number add x y = x + y Return Type 18
  10. 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
  11. 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
  12. Infix Function apply :: forall a b. (a -> b)

    -> a -> b apply f x = f x infixr 0 apply as $ 22
  13. 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
  14. Recursion: fib fib :: Int -> Int fib 0 =

    1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) 27
  15. 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
  16. 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
  17. 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
  18. range infix 8 range as .. > 1 .. 5

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

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

    Array a > concat [[1, 2, 3], [4, 5], [6]] [1, 2, 3, 4, 5, 6] 33
  21. 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
  22. 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
  23. 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
  24. 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
  25. factor (Cont.) > import Data.Foldable > factors n = filter

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

    ..3 … pure i …^D [1,2,3] 40
  27. `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
  28. `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
  29. `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
  30. 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
  31. 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
  32. Tail Recursion (Cont.) > f 0 = 0 > f

    n = 1 + f (n - 1) > f 10 10 > f 100000 RangeError: Maximum call stack size exceeded 47
  33. 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
  34. 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