Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created June 29, 2022 13:44
Show Gist options
  • Save zwaldowski/8a1258765fe57aaa043f6e3021e00096 to your computer and use it in GitHub Desktop.
Save zwaldowski/8a1258765fe57aaa043f6e3021e00096 to your computer and use it in GitHub Desktop.
@resultBuilder
struct ArrayBuilder<Element> {
static func buildExpression(_ expression: Element) -> [Element] {
[ expression ]
}
static func buildBlock(_ elements: [Element]...) -> [Element] {
elements.flatMap { $0 }
}
static func buildOptional(_ elements: [Element]?) -> [Element] {
elements ?? []
}
static func buildEither(first component: [Element]) -> [Element] {
component
}
static func buildEither(second component: [Element]) -> [Element] {
component
}
}
@ArrayBuilder<Int> var bestNumbers: [Int] {
16
42
9001
if Bool.random() {
69
} else {
68
}
if Bool.random() {
101
}
}
print(bestNumbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment