Created
October 13, 2019 14:30
-
-
Save lazyvar/9dc83ef59e0b05eb72130c4d60c3c63c to your computer and use it in GitHub Desktop.
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
// | |
// 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