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."
• 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
• 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
• 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
-> 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
-> 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
| 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
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
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
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
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
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"
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”