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

PureScript Lunch-n-Learn Lesson 7

PureScript Lunch-n-Learn Lesson 7

Slides for the seventh lesson in my PureScript lunch-n-learn series. This lesson concludes our focus on pattern matching, covering case-of expressions, algebraic data types, and newtypes.

Jim Fitzpatrick

August 24, 2016
Tweet

More Decks by Jim Fitzpatrick

Other Decks in Programming

Transcript

  1. • Pick up where we left off on pattern matching,

    covering case expressions, match failure, partial functions, and algebraic data types • Solve related problems in an interactive, “dojo” style Goals
  2. This lesson is built upon Ch. 5 of PureScript by

    Example. Be sure to check it out: https://leanpub.com/purescript/read#leanpub-auto-pattern-matching Note
  3. Pattern Matching: case of -- Simplified palindrome test isPalindrome ::

    String -> Boolean isPalindrome str = case reverseString str of reversed -> str == reversed _ -> false where reverseString :: String -> String reverseString = toCharArray >>> reverse >>> fromCharArray
  4. Pattern Matching: case of -- Simplified palindrome test isPalindrome ::

    String -> Boolean isPalindrome str = case reverseString str of reversed -> str == reversed _ -> false where reverseString :: String -> String reverseString = toCharArray >>> reverse >>> fromCharArray Use case of to pattern match on the result of a computation
  5. • Functions that return a value for any input are

    “total functions” • Functions that do not are “partial functions” • Total functions are preferred where possible Totality
  6. • Functions that return a value for any input are

    “total functions” • Functions that do not are “partial functions” • Total functions are preferred where possible Totality
  7. • Functions that return a value for any input are

    “total functions” • Functions that do not are “partial functions” • Total functions are preferred where possible Totality
  8. Data.Partial > import Data.Partial (unsafePartial) > let partialFunction :: Boolean

    -> Boolean partialFunction = unsafePartial \true -> true > partialFunction true true > partialFunction false Error: Failed pattern match
  9. • Abbreviated ADTs, not to be confused with Abstract Data

    Types • Type-safe way to solve the same sorts of problems that Interfaces or Abstract Classes solve in OO Algebraic Data Types
  10. Algebraic Data Types data Shape = Circle Point Number |

    Rectangle Point Number Number | Line Point Point | Text Point String data Point = Point { x :: Number , y :: Number }
  11. Algebraic Data Types data Shape = Circle Point Number |

    Rectangle Point Number Number | Line Point Point | Text Point String data Point = Point { x :: Number , y :: Number } data keyword for defining an ADT Define a constructor for each possible “shape”
  12. Algebraic Data Types data Shape = Circle Point Number |

    Rectangle Point Number Number | Line Point Point | Text Point String data Point = Point { x :: Number , y :: Number } data keyword for defining an ADT Define a constructor for each possible “shape” Arguments to ADT constructors are not limited to primitive types
  13. Algebraic Data Types data Shape = Circle Point Number |

    Rectangle Point Number Number | Line Point Point | Text Point String data Point = Point { x :: Number , y :: Number } data keyword for defining an ADT Define a constructor for each possible “shape” Arguments to ADT constructors are not limited to primitive types Equally useful for types with a single constructor
  14. Algebraic Data Types data Shape = Circle Point Number |

    Rectangle Point Number Number | Line Point Point | Text Point String data Point = Point { x :: Number , y :: Number } data keyword for defining an ADT Define a constructor for each possible “shape” Arguments to ADT constructors are not limited to primitive types Equally useful for types with a single constructor Constructors may have the same name as the ADT itself
  15. • ADT definitions may be recursive ◦ e.g., data List

    a = Nil | Cons a (List a) • ADT constructors are applied just like functions • Use pattern matching to consume values from an ADT Algebraic Data Types
  16. • ADT definitions may be recursive ◦ e.g., data List

    a = Nil | Cons a (List a) • ADT constructors are applied just like functions • Use pattern matching to consume values from an ADT Algebraic Data Types
  17. • ADT definitions may be recursive ◦ e.g., data List

    a = Nil | Cons a (List a) • ADT constructors are applied just like functions • Use pattern matching to consume values from an ADT Algebraic Data Types
  18. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x: x, y: y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ...
  19. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x: x, y: y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ... Nested pattern matching to extract point values
  20. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x: x, y: y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ... Nested pattern matching to extract point values Pattern match on each shape
  21. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x: x, y: y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ... Nested pattern matching to extract point values Pattern match on each shape Records support same-name destructuring, much like ES6
  22. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x, y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ... Nested pattern matching to extract point values Pattern match on each shape Records support same-name destructuring, much like ES6
  23. Algebraic Data Types showPoint :: Point -> String showPoint (Point

    { x, y }) = "(" <> show x <> ", " <> show y <> ")" showShape :: Shape -> String showShape (Circle p r) = ... showShape (Rectangle p w h) = ... showShape (Line start end) = ... showShape (Text p text) = ... Nested pattern matching to extract point values Pattern match on each shape Records support same-name destructuring, much like ES6 This works for assignment as well.
  24. Algebraic Data Types newtype Pixels = Pixels Number newtype Inches

    = Inches Number Uses the newtype keyword to define a single constructor