Categories
Humor Programming

Demonstrating map, filter, and reduce in Swift using food emoji

In my last article, I posted this graphic, which uses emoji to make it easier to understand what the map, filter, and reduce functions do:

map filter reduce in emoji

Since then, I’ve been asked by a couple of friends if what’s in the graphic is just pseudocode or if it could actually be implemented. I told them it was the latter, and here’s my implementation in Swift:

// Map

func cook(_ item: String) -> String {
  let cookupTable = [
    "🐮": "🍔", // Cow face -> burger
    "🐄": "🍔", // Cow -> burger
    "🐂": "🍖", // Ox -> meat on bone
    "🐷": "🍖", // Pig face -> meat on bone
    "🐽": "🍖", // Pig nose -> meat on bone
    "🐖": "🍖", // Pig -> meat on bone
    "🐑": "🍖", // Sheep -> meat on bone
    "🐐": "🍖", // Goat -> meat on bone
    "🐔": "🍗", // Chicken -> poultry leg
    "🦃": "🍗", // Turkey -> poultry leg
    "🐸": "🍗", // Frog  -> poultry leg (no frog leg emoji...yet)
    "🐟": "🍣", // Fish -> sushi
    "🐠": "🍣", // Tropical fish -> sushi
    "🐡": "🍣", // Blowfish -> sushi
    "🐙": "🍣", // Octopus -> sushi
    "🍠": "🍟", // (Sweet) potato -> French fries
    "🌽": "🍿", // Corn -> popcorn
    "🌾": "🍚", // Rice -> cooked rice
    "🍓": "🍰", // Strawberry -> shortcake
    "🍂": "🍵", // Dried leaves -> tea
  ]
  if let cookedFood = cookupTable[item] {
    return cookedFood
  }
  else {
    return "🍽" // Empty plate
  }
}

let cookedFood = ( ["🐮", "🍠", "⚽️", "🐔", "🌽"].map { cook($0) } )
// cookedFood == ["🍔", "🍟", "🍽", "🍗", "🍿"]


// Filter

func isVegetarian(_ item: String) -> Bool {
  let vegetarianDishes = Set([
    "🍟", // French fries
    "🍿", // Popcorn
    "🍚", // Cooked rice
    "🍰", // Shortcake
    "🍵", // Tea
  ])
  return vegetarianDishes.contains(item)
}

let meatFree = ["🍔", "🍖", "🍟", "🍽", "🍗", "🍿", "🍰"].filter { isVegetarian($0) }
// meatFree == ["🍟", "🍿", "🍰"]


// Reduce

func eat(_ previous: String, _ current: String) -> String {
  let qualifyingFood = Set([
    "🍔", // Burger
    "🍖", // Meat on bone
    "🍗", // Poultry leg
    "🍣", // Sushi
    "🍟", // French fries
    "🍿", // Popcorn
    "🍚", // Cooked rice
    "🍰", // Shortcake
  ])
  if (previous == "" || previous == "💩") && qualifyingFood.contains(current) {
    return "💩" // Poop
  }
  else {
    return ""
  }
}

let aftermath = ["🍔", "🍟", "🍗", "🍿"].reduce("", combine: eat)
// aftermath == "💩"

I put this into a Swift playground, which you can copy from this Gist or download here.

Categories
Hardware Humor

Kirk and Spock travel to 2014; get laughed at for their pitiful mobile devices

kirk and spock in 2014

Click the image to see the full comic on its original page.

Dan Piraro, on his comic strip Bizarro, shows exactly what would happen if the crew of the U.S.S. Enterprise time-traveled to 2014 and people saw their communicators.

Oddly enough, it was Star Trek that inspired Motorola’s Martin Cooper in his work on the first truly mobile phones:

this article also appears in the GSG blog