Skip to content

Instantly share code, notes, and snippets.

@TsRebornz
Created November 13, 2022 08:34
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 TsRebornz/409ae56f7349624d32fad86b2dd03e06 to your computer and use it in GitHub Desktop.
Save TsRebornz/409ae56f7349624d32fad86b2dd03e06 to your computer and use it in GitHub Desktop.
struct Size {
var width: Int
let height: Int
}
enum Material: String {
case wood
case iron
}
struct Table {
var size: Size
let material: Material
}
extension Table {
var isWood: Bool {
return material == .wood
}
}
let tables: [Table] = [
.init(
size: .init(width: 150, height: 10),
material: .iron
),
.init(
size: .init(width: 200, height: 10),
material: .iron
),
.init(
size: .init(width: 100, height: 10),
material: .wood
)
]
// Mapping
let tableMaterials = tables.map(\.material)
// Filtering
let woodTables = tables.filter(\.isWood)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment