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
let admins = [User(name: "Admin")] | |
// [{name "Admin"}] | |
let guests = [User(name: "Guest")] | |
// [{name "Guest"}] | |
var allUsers = admins + guests | |
// [{name "Admin"}, {name "Guest"}] | |
// TODO: - Iterate over the entire collection with the for-in loop: | |
for user in allUsers { |
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
for (index, user) in allUsers.enumerated() { | |
print("User: \(user) at index: \(index)") | |
/* | |
User: User(name: "Admin") at index: 0 | |
User: User(name: "Guest") at index: 1 | |
*/ | |
} |
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
// MARK: - Comparable | |
extension User: Comparable { | |
static func <(lhs: User, rhs: User) -> Bool { | |
return lhs.name < rhs.name | |
} | |
static func ==(lhs: User, rhs: User) -> Bool { | |
return lhs.name == rhs.name | |
} |
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
let incommingMessages: [String] = [] | |
print("emptiness: \(incommingMessages.isEmpty)") | |
print("counting: \(incommingMessages.count == 0)") |
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
let messageIDs = Array<Int>(1...1000000) | |
let contiguousMessageIDs = ContiguousArray<Int>(1...1000000) | |
var start = CFAbsoluteTimeGetCurrent() | |
messageIDs.reduce(0, +) | |
var end = CFAbsoluteTimeGetCurrent() - start | |
print("Array reduce Took \(end) seconds") | |
// "Array reduce Took 0.203467965126038 seconds\n" | |
start = CFAbsoluteTimeGetCurrent() |
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
struct User { | |
let id: Int | |
let name: String | |
} | |
var users: Set<User> = [] | |
// error: type 'User' does not confirm to protocol 'Hashable' |
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
struct User { | |
let id: Int | |
let name: String | |
} | |
extension User: Hashable { | |
var hashValue: Int { | |
return id | |
} |
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
var users: Set<User> = [] | |
let user = User(id: 65, name: "Maxim Vialyx") | |
users.insert(user) | |
// (inserted true, {id 65, name "Maxim Vialyx"}) | |
users.insert(user) | |
// (inserted false, {id 65, name "Maxim Vialyx"}) | |
users.remove(user) | |
// Return removed user and Set now is empty Set([]) |
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
// TODO: - Use .isEmpty instead of .count | |
if allUsers.isEmpty { | |
print("User list is empty") | |
} else { | |
let user = allUsers.first | |
// NOT subscript. Set is unordered collection | |
// user = allUsers[0] | |
print("First user is: \(user)") | |
// "First user is: Optional(__lldb_expr_209.User(id: 654, name: "Chilly Mango"))\n" | |
} |
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
// TODO: - Iterate over the entire with the for-in loop: | |
for user in allUsers { | |
print("User: \(user)") | |
/* | |
User: User(id: 654, name: "Chilly Mango") | |
User: User(id: 7, name: "Mike Biggles") | |
User: User(id: 234, name: "Midum Premium") | |
User: User(id: 99, name: "Jimmy Cho") | |
*/ | |
} |