Skip to content

Instantly share code, notes, and snippets.

@jsorge
Last active December 1, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsorge/140d42e710b5f39f59ad to your computer and use it in GitHub Desktop.
Save jsorge/140d42e710b5f39f59ad to your computer and use it in GitHub Desktop.
Trying to work out how to remove these nasty loops...
//: Playground - noun: a place where people can play
import UIKit
struct Person {
let name: String
let isCool: Bool?
}
struct Department {
let name: String
let workers: [Person]
}
let tech = Department(name: "Tech", workers: [Person(name: "Jared", isCool: true), Person(name: "Emily", isCool: true), Person(name: "Atticus", isCool: true), Person(name: "Josh", isCool: nil)])
let facilities = Department(name: "Facilities", workers: [Person(name: "Aaron", isCool: true), Person(name: "Joe", isCool: nil), Person(name: "Alex", isCool: nil), Person(name: "Will", isCool: true)])
let company = [tech, facilities]
//I want to remove lines 22 - 29, and collapse them into 1 declaration:
//It would be `let coolPeople = ...`
var coolPeople = [Person]()
for department in company {
for person in department.workers {
if let _ = person.isCool {
coolPeople.append(person)
}
}
}
print(coolPeople)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment