๐ paiza.io
struct User {
let email: String
let isActive: Bool
}
let users = [
User(email: "joe@me.com", isActive: true),
User(email: "joy@me.com", isActive: false)
]
// โญ๏ธ key path as functions
// 1. \.email = { $0.email }
// 2. \.isActive = { $0.isActive }
// โญโโ1โโโฎ
let emails = users.map(\.email) // (User) -> String
// ["joe@me.com", "joy@me.com"]
// โญโโโ2โโโโโฎ
let activeUsers = users.filter(\.isActive) // (User) -> Bool
// [User(email: "joe@me.com", isActive: true)]
// โญ๏ธ subscript <---> key path
// user[keyPath: \User.isActive] == user.isActive
// let userStates = users.map { user in user[keyPath: \User.isActive] } // == users.map { $0.isActive }