Skip to content

Instantly share code, notes, and snippets.

@themathmagician
Last active March 25, 2018 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themathmagician/bfead55319bda565090de259999cd7d8 to your computer and use it in GitHub Desktop.
Save themathmagician/bfead55319bda565090de259999cd7d8 to your computer and use it in GitHub Desktop.
// Map a list to a frequency list by using groupBy with identity and mapValues
val chars = "Peace and harmony"
chars.groupBy(identity).mapValues(_.size).toList
//Use a tupple: type to pass in two arguments for pattern matching, like goldilocks("porridge", "Mama")
def goldilocks(tuple: (String, String)) = tuple match {
case ("porridge", "Papa") ⇒ "Papa eating porridge"
case ("porridge", "Mama") ⇒ "Mama eating porridge"
case ("porridge", "Baby") ⇒ "Baby eating porridge"
case _ ⇒ "what?"
} //> goldilocks: (tuple: (String, String))String
assert(goldilocks("porridge", "Mama") == "Mama eating porridge")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment