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

Scala: Welcome To The Wonderful World Of Functi...

Scala: Welcome To The Wonderful World Of Functional Programming

Screencasts are here: https://vimeo.com/album/5111827

Jennifer Konikowski

April 13, 2018
Tweet

More Decks by Jennifer Konikowski

Other Decks in Programming

Transcript

  1. Why Functional Programming? • use side-effect-free functions as a basic

    building block in the language • order of execution is not important (useful for concurrency) • easy to translate mathematical functions into functional languages
  2. QUESTION Create a function that allows you to take in

    two strings and append them together
  3. Map • Holds basic datatypes • Key-value pairs • Can

    contain Maps or other functions as values • Immutable
  4. Option Scala doesn’t like using null. Instead, we use None.

    If there’s a chance of a value being None, then use Option Basic interface:
  5. Either Similar to Option, but it has two sub- types:

    Left and Right. It’s regularly used in error handling with Left being the error and Right being the success
  6. Map Evaluates a function over each element in the list,

    returning a list with the same number of elements. or pass a function!
  7. • is just like a regular class (with a few

    key differences) • is good for modeling immutable data • requires the keywords case class, an identifier, and a parameter list (which may be empty) A case class:
  8. Case Class Example When you create a case class with

    parameters, the parameters are public vals
  9. Pattern Matching Checks a value against a pattern and a

    successful match can also deconstruct a value into its constituent parts
  10. QUESTION • Create case classes of animals (I used Dogs,

    Cats, and Snakes) and a function called 'speak'. • 'speak' should return the first two animals' speaking function. • For example, if you have Dog, there should be a function 'bark' that 'speak' returns. • For any other type, return "???"
  11. Partially Applied Function Name sounds the same, but it's very

    different! This allows you to create one function out of another using _ as a wildcard