Original idea can be found here.
Created
October 4, 2016 06:43
-
-
Save sjednac/9ae64d3d1da23c97ae627d70d59158d5 to your computer and use it in GitHub Desktop.
Map, filter and foldLeft using an Emoji example in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object EmojiMeal extends App { | |
type Ingredient = String | |
type Meal = String | |
type Person = String | |
type Poop = String | |
def cook(item: Ingredient): Meal = item match { | |
case "๐ฎ" => "๐" | |
case "๐ " => "๐" | |
case "๐" => "๐" | |
case "๐ฝ" => "๐ฟ" | |
case _ => "๐ฒ" | |
} | |
def isVegetarian(meal: Meal): Boolean = meal match { | |
case "๐" => true | |
case "๐ฟ" => true | |
case _ => false | |
} | |
def eat(person: Person, meal: Meal): Poop = "๐ฉ" | |
val ingredients = List("๐ฎ", "๐ ", "๐", "๐ฝ") | |
println(s"Ingredients: ${ingredients.mkString(",")}") | |
val meals = ingredients.map(cook) | |
println(s"Meals: ${meals.mkString(",")}") | |
val vegetarianMeals = meals.filter(isVegetarian) | |
println(s"Vegetarian meals: ${vegetarianMeals.mkString(",")}") | |
val eaten = vegetarianMeals.foldLeft("๐")(eat) | |
println(s"Eaten: ${eaten}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment