This file contains 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
let dateFormatter = NSDateFormatter() | |
dateFormatter.dateFormat = "MMMM d" | |
private func daySuffix(date: NSDate) -> String { | |
let calendar = NSCalendar.currentCalendar() | |
let dayOfMonth = calendar.component(.Day, fromDate: date) | |
switch dayOfMonth { | |
case 1: fallthrough | |
case 21: fallthrough | |
case 31: return "st" |
This file contains 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
func romanNumerals(input: Int) -> String { | |
var number = input | |
let numerals = ["M", "M", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] | |
let values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] | |
var numeralString = "" | |
for (i, numeral) in numerals.enumerate() { | |
while number >= values[i] { | |
number -= values[i] | |
numeralString += numeral |
This file contains 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
var sketch = context.api() | |
function moveForward(layer, amount) { | |
for (var i = 0; i < amount; ++i) { | |
layer.moveForward() | |
} | |
} | |
function organizeLayers(prefix, layers) { | |
var sortedLayers = []; |
This file contains 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
import HTTP | |
import Vapor | |
extension Droplet { | |
func index(of middlewareType: Middleware.Type) -> Int? { | |
for (i, middleware) in self.middleware.enumerated() { | |
if type(of: middleware) == middlewareType.self { | |
return i | |
} |
This file contains 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
enum Food: String { | |
case beef = "Beef" | |
case chicken = "Chicken" | |
case vegitarian = "Vegitarian" | |
case kids = "Kids" | |
} | |
struct Person { | |
let name: String | |
let food: Food |
This file contains 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
enum Food: String { | |
case beef = "Beef" | |
case chicken = "Chicken" | |
case vegitarian = "Vegitarian" | |
case kids = "Kids" | |
} | |
struct Person { | |
let name: String | |
let food: Food |
This file contains 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
Table { | |
Family("Mr. & Mrs. Smith") { | |
Person("Mr. John Smith", .beef) | |
Person("Mrs. Jane Smith", .chicken) | |
} | |
Person("Mr. Ed Ford", .chicken) | |
Person("Mrs. Amanda Doolittle", .vegitarian) | |
Family("Mr. & Mrs. Appleseed") { | |
Person("Mr. Jonny Appleseed", .chicken) | |
Person("Mrs. Jessica Appleseed", .vegitarian) |
This file contains 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
@_functionBuilder | |
struct PeopleBuilder { | |
static func buildBlock(_ people: Person...) -> [Person] { | |
return people | |
} | |
} | |
struct Table { | |
let people: [Person] | |
This file contains 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
protocol People { | |
var people: [Person] { get } | |
} | |
@_functionBuilder | |
struct PeopleBuilder { | |
static func buildBlock(_ people: People...) -> [Person] { | |
return people.reduce([], {$0+$1.people}) | |
} | |
} |
This file contains 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
for (i, table) in tables.enumerated() { | |
print("Table #\(i+1)") | |
print(table.people | |
.map{"\($0.name),\($0.food.rawValue)"} | |
.joined(separator: "\n") | |
) | |
} |