Skip to content

Instantly share code, notes, and snippets.

@Alexander-Ignition
Last active May 13, 2021 14:36
Show Gist options
  • Save Alexander-Ignition/e8739956c2f440fd90c8ea6668458433 to your computer and use it in GitHub Desktop.
Save Alexander-Ignition/e8739956c2f440fd90c8ea6668458433 to your computer and use it in GitHub Desktop.
Simple Swift function builder
@resultBuilder
enum ArrayBuilder {
@inlinable
static func buildExpression<T>(_ expression: T) -> [T] {
[expression]
}
@inlinable
static func buildBlock<T>(_ components: [T]...) -> [T] {
components.flatMap { $0 }
}
@inlinable
static func buildOptional<T>(_ component: [T]?) -> [T] {
component ?? []
}
@inlinable
static func buildArray<T>(_ components: [[T]]) -> [T] {
components.flatMap { $0 }
}
}
let flag = true
@ArrayBuilder
var numbers: [Int] {
1
2
if flag {
3
4
}
for i in 5..<10 {
i
0
}
10
11
}
print(numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment