Skip to content

Instantly share code, notes, and snippets.

@lazyvar
Created October 13, 2019 14:30
Show Gist options
  • Save lazyvar/9dc83ef59e0b05eb72130c4d60c3c63c to your computer and use it in GitHub Desktop.
Save lazyvar/9dc83ef59e0b05eb72130c4d60c3c63c to your computer and use it in GitHub Desktop.
//
// main.swift
// Mad
//
// Created by Mack on 10/13/19.
// Copyright © 2019 Mack. All rights reserved.
//
import Foundation
func prompt(_ request: String) -> String! { print(request); return readLine() }
enum FillIn: String {
case name, adjective, noun
}
enum Lib {
case words(String)
case fillIn(FillIn)
func promptedDescription() -> String {
switch self {
case .words(let words):
return words
case .fillIn(let fillIn):
return prompt("Can I get a \(fillIn.rawValue)?")
}
}
}
typealias MadLib = [Lib]
extension MadLib {
func sentence() -> String {
return self
.map { $0.promptedDescription() }
.joined(separator: " ")
}
}
let excused: MadLib = [
.words("Please excuse"),
.fillIn(.name),
.words("who is far too"),
.fillIn(.adjective),
.words("to attend"),
.fillIn(.noun),
.words("class.")
]
print(excused.sentence())
// Can I get a name?
// Mack
// Can I get a adjective?
// hot
// Can I get a noun?
// spoon
// Please excuse Mack who is far too hot to attend spoon class.
// Program ended with exit code: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment