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

PureScript Lunch-n-Learn Lesson 8

PureScript Lunch-n-Learn Lesson 8

Slides for the eigth lesson in my PureScript lunch-n-learn series. This lesson covers the first part of the "PureScript by Example" book chapter 6, "Type Classes."

Jim Fitzpatrick

September 01, 2016
Tweet

More Decks by Jim Fitzpatrick

Other Decks in Programming

Transcript

  1. • Gain familiarity with type classes • Solve related problems

    in an interactive, “dojo” style Goals
  2. This lesson is built upon Ch. 6 of PureScript by

    Example. Be sure to check it out: https://leanpub.com/purescript/read#leanpub-auto-type-classes Note
  3. • Use the class keyword to define a type class

    • Type class definitions define the class name and all expected arguments • A type class instance contains implementations of the functions defined in the type class, specialized to a particular type Type Classes
  4. • Use the class keyword to define a type class

    • Type class definitions define the class name and all expected arguments • A type class instance contains implementations of the functions defined in the type class, specialized to a particular type Type Classes
  5. • Use the class keyword to define a type class

    • Type class definitions define the class name and all expected arguments • A type class instance contains implementations of the functions defined in the type class, specialized to a particular type Type Classes
  6. Type Classes: example class Show a where show :: a

    -> String instance showBoolean :: Show Boolean where show true = "true" show false = "false"
  7. Type Classes: example class Show a where show :: a

    -> String instance showBoolean :: Show Boolean where show true = "true" show false = "false" Use the class keyword to define a type class
  8. Type Classes: example class Show a where show :: a

    -> String instance showBoolean :: Show Boolean where show true = "true" show false = "false" Use the class keyword to define a type class Define signatures for the function(s) that instances of the type class will implement
  9. Type Classes: example class Show a where show :: a

    -> String instance showBoolean :: Show Boolean where show true = "true" show false = "false" Use the class keyword to define a type class Define signatures for the function(s) that instances of the type class will implement Use the instance keyword to define instances of a type class
  10. Common Type Classes: Eq class Eq a where eq ::

    a -> a -> Boolean > 1 == 2 false > "Test" == "Test" true • Test two values for equality • Aliased with ==
  11. Common Type Classes: Ord data Ordering = LT | EQ

    | GT class (Eq a) <= Ord a where compare :: a -> a -> Ordering > compare 1 2 LT > compare "A" "Z" LT • Compare two values for ordering • Comparison operators can be defined in terms of compare
  12. Common Type Classes: Field class EuclideanRing a <= Field a

    • Types that support numeric operators (e.g., addition, etc.) • We’ll talk about this more when we cover superclasses
  13. Common Type Classes: Semigroup class Semigroup a where append ::

    a -> a -> a • Types that support an append operation, e.g., strings, arrays, etc. • The <> operator is an alias for append
  14. Common Type Classes: Monoid class Semigroup m <= Monoid m

    where mempty :: m > import Data.Monoid > import Data.Foldable > foldl append mempty ["Hello", " ", "World"] "Hello World" > foldl append mempty [[1, 2, 3], [4, 5], [6]] [1,2,3,4,5,6] • Semigroups that have the concept of an “empty” value • Describes how to accumulate a value by starting with an empty value and folding
  15. • Monoid identifies types which act as the result of

    a fold; Foldable identifies constructors that are the source of a fold • Defines foldl, foldr, and foldMap • Many types may be folded; not just Strings and Arrays. See purescript-foldable-traversable Common Type Classes: Foldable
  16. • Monoid identifies types which act as the result of

    a fold; Foldable identifies constructors that are the source of a fold • Defines foldl, foldr, and foldMap • Many types may be folded; not just Strings and Arrays. See purescript-foldable-traversable Common Type Classes: Foldable
  17. • Monoid identifies types which act as the result of

    a fold; Foldable identifies constructors that are the source of a fold • Defines foldl, foldr, and foldMap • Many types may be folded; not just Strings and Arrays. See purescript-foldable-traversable Common Type Classes: Foldable
  18. Common Type Classes: Foldable class Foldable f where foldr ::

    forall a b. (a -> b -> b) -> b -> f a -> b foldl :: forall a b. (b -> a -> b) -> b -> f a -> b foldMap :: forall a m. Monoid m => (a -> m) -> f a -> m > import Data.Foldable > foldMap show [1, 2, 3, 4, 5] "12345"
  19. Common Type Classes: Functor class Functor f where map ::

    forall a b. (a -> b) -> f a -> f b > import Prelude > map (\n -> n < 3) [1, 2, 3, 4, 5] [true, true, false, false, false] > import Data.Maybe > import Data.String (length) > map length (Just "testing") (Just 7) • “Lift” functions over data structures • Map aliased as <$> • Captures the idea of applying a function to the elements of a container and returning a container of the same “shape”