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

PureScript Lunch-n-Learn Lesson 2

PureScript Lunch-n-Learn Lesson 2

Slides for the second lesson in my PureScript lunch-n-learn series. The topic is "Records and Functions," and the material builds upon chapter 3 of "PureScript by Example."

Jim Fitzpatrick

June 30, 2016
Tweet

More Decks by Jim Fitzpatrick

Other Decks in Programming

Transcript

  1. • Take a closer look at records and functions •

    Build an address book program, “dojo style” Goals
  2. This lesson is built upon Ch. 3 of PureScript by

    Example. Be sure to check it out: https://leanpub.com/purescript/read#leanpub-auto-functions-and-records Note
  3. • Translate directly to JavaScript objects • Contain zero or

    more fields • Values may be heterogeneous Records
  4. • Translate directly to JavaScript objects • Contain zero or

    more fields • Values may be heterogeneous Records
  5. • Translate directly to JavaScript objects • Contain zero or

    more fields • Values may be heterogeneous Records
  6. Records > let person = { name: "Jim" , interests:

    [ "Functional Programming" , "League of Legends" ] } > :type person { name :: String , interests :: Array String }
  7. Records Access fields using dot notation: > person.name “Jim” >

    person.interests [“Functional Programming”, “League of Legends”]
  8. Records Defining a type: type Address = { street ::

    String , city :: String , state :: String } type Entry = { firstName :: String , lastName :: String , address :: Address } type AddressBook = List Entry
  9. Records Defining a type: type Address = { street ::

    String , city :: String , state :: String } type Entry = { firstName :: String , lastName :: String , address :: Address } type AddressBook = List Entry Use the type keyword to define a type
  10. Records Defining a type: type Address = { street ::

    String , city :: String , state :: String } type Entry = { firstName :: String , lastName :: String , address :: Address } type AddressBook = List Entry Use the type keyword to define a type Here we quantify the types of our record fields
  11. Records Defining a type: type Address = { street ::

    String , city :: String , state :: String } type Entry = { firstName :: String , lastName :: String , address :: Address } type AddressBook = List Entry Use the type keyword to define a type Here we quantify the types of our record fields Types can be nested
  12. Records Defining a type: type Address = { street ::

    String , city :: String , state :: String } type Entry = { firstName :: String , lastName :: String , address :: Address } type AddressBook = List Entry Use the type keyword to define a type Here we quantify the types of our record fields Types can be nested Linked list of Entries
  13. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  14. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  15. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  16. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  17. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  18. • Almost everything (even operators!) is a function • To

    call a function, supply the function name and all parameters, separated by spaces • Functions are left-associative • All functions are curried • Use parens (or the $ operator) to nest calls as needed • Translate directly to JavaScript functions Functions
  19. -- Convert an Address to a string for display purposes.

    showAddress :: Address -> String showAddress addr = addr.street <> ", " <> addr.city <> ", " <> addr.state > showAddress { street: "123 Roads Rd.", city: "Philadelphia", state: "PA" } "123 Roads Rd., Philadelphia, PA" Functions
  20. Functions -- Convert a full Entry to a string for

    display purposes. showEntry :: Entry -> String showEntry entry = entry.lastName <> ", " <> entry.firstName <> ": " <> showAddress entry.address > showEntry entry "Fitzpatrick, Jim: 123 Roads Rd., Philadelphia, PA"
  21. Cons • Comes from Data.List (purescript-lists) • Is a prepend

    operation for linked lists • Type: Cons :: forall a. a -> List a -> List a Functions
  22. Functions -- Given an Entry and an AddressBook, return a

    copy of the AddressBook that -- contains the given entry. insertEntry :: Entry -> AddressBook -> AddressBook insertEntry entry book = Cons entry book
  23. Functions -- Given an Entry and an AddressBook, return a

    copy of the AddressBook that -- contains the given entry. insertEntry :: Entry -> AddressBook -> AddressBook insertEntry entry book = Cons entry book
  24. Functions -- Given an Entry and an AddressBook, return a

    copy of the AddressBook that -- contains the given entry. insertEntry :: Entry -> AddressBook -> AddressBook insertEntry entry = Cons entry
  25. Functions -- Given an Entry and an AddressBook, return a

    copy of the AddressBook that -- contains the given entry. insertEntry :: Entry -> AddressBook -> AddressBook insertEntry entry = Cons entry
  26. Functions -- Given an Entry and an AddressBook, return a

    copy of the AddressBook that -- contains the given entry. insertEntry :: Entry -> AddressBook -> AddressBook insertEntry = Cons
  27. Functions -- Given a firstName, lastName, and AddressBook, return the

    first entry in the -- AddressBook that contains the given first and last names. Since it may be -- the case that no entries match, this function is "safe" and return Maybe Entry. findEntry firstName lastName book = head $ filter containsName book where containsName entry = entry.firstName == firstName && entry.lastName == lastName
  28. Functions -- Given a firstName, lastName, and AddressBook, return the

    first entry in the -- AddressBook that contains the given first and last names. Since it may be -- the case that no entries match, this function is "safe" and return Maybe Entry. findEntry firstName lastName = head <<< filter containsName where containsName entry = entry.firstName == firstName && entry.lastName == lastName
  29. Functions -- Given a firstName, lastName, and AddressBook, return the

    first entry in the -- AddressBook that contains the given first and last names. Since it may be -- the case that no entries match, this function is "safe" and return Maybe Entry. findEntry firstName lastName = filter containsName >>> head where containsName entry = entry.firstName == firstName && entry.lastName == lastName