// Map, filter, and reduce in Swift, explained with emoji
// https://gist.github.com/AccordionGuy/61716adbf706801e2a496a12ff19526e
// http://devhumor.com/media/map-filter-reduce-explained-with-emoji-s
// http://unicodey.com/emoji-data/table.htm
// Map
function cook(item) {
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 (cookupTable[item] != undefined) {
return cookupTable[item];
} else {
return '๐ฝ'; // Empty plate
}
}
let cookedFood = ['๐ฎ', '๐ ', 'โฝ๏ธ', '๐', '๐ฝ'].map(item => cook(item));
console
.log(cookedFood) // ["๐", "๐", "๐ฝ", "๐", "๐ฟ"]
[('๐ฝ', '๐ฎ', '๐')].map(item => cook(item)); // [ ๐ฟ , ๐ , ๐ณ ]
// Filter
function isVegetarian(item) {
let vegetarianDishes = [
'๐', // French fries
'๐ฟ', // Popcorn
'๐', // Cooked rice
'๐ฐ', // Shortcake
'๐ต', // Tea
];
return vegetarianDishes.includes(item);
}
let meatFree = ['๐', '๐', '๐', '๐ฝ', '๐', '๐ฟ', '๐ฐ'].filter(item => isVegetarian(item));
console
.log(meatFree) // ["๐", "๐ฟ", "๐ฐ"]
[('๐ฟ', '๐', '๐ณ')].filter(item => isVegetarian(item)); // [๐ฟ, ๐ณ]
// Reduce
function eat(previous, current) {
let qualifyingFood = [
'๐', // Burger
'๐', // Meat on bone
'๐', // Poultry leg
'๐ฃ', // Sushi
'๐', // French fries
'๐ฟ', // Popcorn
'๐', // Cooked rice
'๐ฐ', // Shortcake
];
if ((previous == '' || previous == '๐ฉ') && qualifyingFood.includes(current)) {
return '๐ฉ'; // Poop
} else {
return '';
}
}
let aftermath = ['๐', '๐', '๐', '๐ฟ'].reduce((previous, current) => eat(previous, current));
console
.log(aftermath) // "๐ฉ"
[('๐ฟ', '๐ณ')].reduce((previous, current) => eat(previous, current)); // ๐ฉ
// http://nomadev.com.br/javascript-emoticons-emojis/
// :man-heart-man:
// :man-woman-boy:
// :man-woman-girl-boy:
'๐จโโค๏ธโ๐จ'.replace(/๐จ/, 'JS'); // "JSโโค๏ธโ๐จ"
'๐จโโค๏ธโ๐จ'.match(/./gu); // ["๐จ", "โ", "โค", "๏ธ", "โ", "๐จ"]
'๐ฉโโค๏ธโ๐ฉ'.match(/./gu); // ["๐ฉ", "โ", "โค", "๏ธ", "โ", "๐ฉ"]
'๐จโโค๏ธโ๐จ'.replace(/๐จ/, '๐ฉ'); // "๐ฉโโค๏ธโ๐จ"
'๐จโโค๏ธโ๐จ'.replace(/๐จ/g, '๐ฉ'); // "๐ฉโโค๏ธโ๐ฉ"
'๐จโ๐ฉโ๐งโ๐ฆ'.match(/./gu); // ["๐จ", "โ", "๐ฉ", "โ", "๐ง", "โ", "๐ฆ"]
'๐จโ๐ฉโ๐งโ๐ฆ'.replace(/๐จ/g, '๐ฉ'); // "๐ฉโ๐ฉโ๐งโ๐ฆ"