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 Randomizer { | |
func randomNumber(upperBound: UInt32) -> UInt32 | |
} | |
class DefaultRandomizer: Randomizer { | |
func randomNumber(upperBound: UInt32) -> UInt32 { | |
return arc4random_uniform(upperBound) | |
} | |
} |
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
class CardGame { | |
private let deck: Deck | |
private let randomizer: Randomizer | |
init(deck: Deck, randomizer: Randomizer = DefaultRandomizer()) { | |
self.deck = deck | |
self.randomizer = randomizer | |
} | |
func drawRandomCard() -> Card { |
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
extension User { | |
func toJSON() -> [String : Any] { | |
var json = [String : Any]() | |
json["firstName"] = firstName | |
json["lastName"] = lastName | |
json["age"] = age | |
json["friends"] = friends.map { $0.toJSON() } | |
json["coworkers"] = coworkers.map { $0.toJSON() } | |
json["favorites"] = favorites.map { $0.toJSON() } | |
json["messages"] = messages.map { $0.toJSON() } |
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
extension User { | |
func toJSON() -> [String : Any] | |
return [ | |
"firstName": firstName, | |
"lastName": lastName, | |
"age": age, | |
"friends": friends.map { $0.toJSON() }, | |
"coworkers": coworkers.map { $0.toJSON() }, | |
"favorites": favorites.map { $0.toJSON() }, | |
"messages": messages.map { $0.toJSON() }, |
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
extension IntegerConvertible { | |
func add<T: IntegerConvertible>(_ number: T) -> CustomNumber { | |
return CustomNumber(int: int + number.int) | |
} | |
} | |
func addNumbers() -> CustomNumber { | |
return CustomNumber(int: 1).add(CustomNumber(int: 2)) | |
.add(CustomNumber(int: 3)) | |
.add(CustomNumber(int: 4)) |
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 addNumbers() -> CustomNumber { | |
return CustomNumber(int: 1) + | |
CustomNumber(int: 2) + | |
CustomNumber(int: 3) + | |
CustomNumber(int: 4) + | |
CustomNumber(int: 5) | |
} |
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 +<A: IntegerConvertible, B: IntegerConvertible>(lhs: A, rhs: B) -> CustomNumber { | |
return CustomNumber(int: lhs.int + rhs.int) | |
} |
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
startime=$(<buildtimes.log) | |
endtime=$(date +%s) | |
deltatime=$((endtime-startime)) | |
newline=$'\n' | |
echo "[Start] $startime$newline[End] $endtime$newline[Delta] $deltatime" > "buildtimes.log" |
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
echo "$(date +%s)" > "buildtimes.log" |
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
class ModelLoader { | |
func loadAllModels() -> AnySequence<Model> { | |
return AnySequence { () -> AnyIterator<Model> in | |
var index = 0 | |
return AnyIterator { | |
let model = database.model(at: index) | |
index += 1 | |
return model | |
} |