Instantly share code, notes, and snippets.

@jsorge jsorge/swift-map.swift
Last active Dec 1, 2015

Embed
What would you like to do?
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