Skip to content

Instantly share code, notes, and snippets.

View GrantJEmerson's full-sized avatar

Grant Emerson GrantJEmerson

View GitHub Profile
import Foundation
struct PreferenceKey<Value>: RawRepresentable {
typealias RawValue = String
let rawValue: RawValue
init (_ key: String) {
rawValue = key
}
@GrantJEmerson
GrantJEmerson / KeyingIntoTuples.swift
Last active November 23, 2019 22:42
Keying Into Tuples - SwiftMoji Entry #1
struct HotDog {
var bun: String?
var meat: String?
var condiments: (chili: String?, cheese: String?, tomatoes: String?)
var ingredients: String {
[bun, meat, condiments.chili, condiments.cheese, condiments.tomatoes]
.compactMap { $0 }
.joined(separator: ", ")
}
@GrantJEmerson
GrantJEmerson / ZipAnArray.swift
Last active November 23, 2019 22:42
Zip-A-Dee-Doo-Dah Zip-An-Array - SwiftMoji Entry #2
typealias Meal = (drink: String, food: String)
let drinks = ["🥛", "☕️", "🥤", "🍺", "🍷", "🍹"]
let foods = ["🍪", "🥓🍳", "🍔🍟", "🥜", "🍝", "🥗"]
let meals = Array(zip(drinks, foods)) as [Meal]
let dinner = meals[2]
print("Tonight, we will have \(dinner.drink) and \(dinner.food) for dinner.")
// Tonight, we will have 🥤 and 🍔🍟 for dinner.
@GrantJEmerson
GrantJEmerson / ThePowerOfReduce.swift
Last active November 23, 2019 22:41
The Power of Reduce - SwiftMoji Entry #3
import Foundation
typealias AnimalGroup = (name: String, size: Int)
let animalGroups: [String: AnimalGroup] = [
"🐬": ("pod", 12),
"🐺": ("pack", 6),
"🦒": ("tower", 10)
]
@GrantJEmerson
GrantJEmerson / LetsBuildAnInvisibleWall.swift
Last active November 23, 2019 22:41
Let's Build an Invisible Wall - SwiftMoji Entry #4
struct Item {
let name: String
let isNecessary: Bool
let price: Int
}
var 🛒 = [
Item(name: "📱", isNecessary: false, price: 600),
Item(name: "🧻", isNecessary: true, price: 10),
Item(name: "🍎", isNecessary: true, price: 5),
@GrantJEmerson
GrantJEmerson / NoSweatForOptionSet.swift
Last active November 23, 2019 22:41
No Sweat for OptionSet - SwiftMoji Entry #5
struct 🎒Items: OptionSet {
let rawValue: Int
static let 💻 = 🎒Items(rawValue: 1 << 0)
static let 📓 = 🎒Items(rawValue: 1 << 1)
static let 📷 = 🎒Items(rawValue: 1 << 2)
static let 🔦 = 🎒Items(rawValue: 1 << 3)
static let 🍎 = 🎒Items(rawValue: 1 << 4)
static let school: 🎒Items = [.💻, .📓]
@GrantJEmerson
GrantJEmerson / IndirectCases.swift
Created November 23, 2019 22:40
Indirect Cases - SwiftMoji Entry #6
indirect enum Folder {
case 📁(label: String, contents: [Folder])
case 📄(String)
}
let models = Folder.📁(label: "Models", contents: [.📄("Experience.swift")])
let views = Folder.📁(label: "Views", contents: [.📄("MapperView.swift")])
let controllers = Folder.📁(label: "Controllers", contents: [.📄("MainViewController.swift")])
let xcodeProject = Folder.📁(label: "Business App", contents: [models, views, controllers])
@GrantJEmerson
GrantJEmerson / SameStringDifferentDiacritics.swift
Created November 24, 2019 00:30
Same String, Different Diacritics - SwiftMoji Entry #7
import Foundation
struct 👤 {
var name: String
var comparisonName: String {
name.folding(options: [.diacriticInsensitive, .widthInsensitive, .caseInsensitive],
locale: .current)
}
}
@GrantJEmerson
GrantJEmerson / WhatsInAName.swift
Created November 24, 2019 00:37
What's in a Name - SwiftMoji Entry #8
import Foundation
var 👨‍👩‍👧‍👦 = """
Family:
Father's Name: Jayson
Mother's Name: Kathrine
Daughter's Name: Alannah
Son's Name: Jacob
"""
@GrantJEmerson
GrantJEmerson / Uniformity.swift
Created November 24, 2019 00:44
Uniformity - SwiftMoji Entry #9
enum Animal {
case 🐶, 🕷, 🐙, 🦁, 🐊
}
var animals: [Animal] = [.🐶, .🕷, .🐶, .🐊, .🐙, .🐶, .🦁]
animals.removeAll { $0 != .🐶 }
if animals.allSatisfy({ $0 == .🐶 }) {
print("Yay! You removed all the scary animals!")
}