Forked from LeeKahSeng/Result-Builders-Basics-Full.swift
Created
March 18, 2021 03:23
-
-
Save wildthink/2d6943270e3de59adad8fd7bcc33f503 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@resultBuilder | |
struct StringBuilder { | |
static func buildBlock(_ components: String...) -> String { | |
let filtered = components.filter { $0 != "" } | |
return filtered.joined(separator: "⭐️") | |
} | |
static func buildOptional(_ component: String?) -> String { | |
return component ?? "" | |
} | |
static func buildEither(first component: String) -> String { | |
return component | |
} | |
static func buildEither(second component: String) -> String { | |
return component | |
} | |
static func buildArray(_ components: [String]) -> String { | |
return components.joined(separator: "") | |
} | |
static func buildExpression(_ expression: Int) -> String { | |
return "\(expression)" | |
} | |
static func buildExpression(_ expression: String) -> String { | |
return expression | |
} | |
static func buildFinalResult(_ component: String) -> Int { | |
return component.count | |
} | |
static func buildFinalResult(_ component: String) -> String { | |
return component | |
} | |
} | |
@StringBuilder func greet(name: String, countdown: Int) -> String { | |
for i in (0...countdown).reversed() { | |
i | |
} | |
"Hello" | |
if !name.isEmpty { | |
"to" | |
name | |
} else { | |
"World" | |
} | |
} | |
print(greet(name: "Swift Senpai", countdown: 5)) | |
@StringBuilder var greetCharCount: Int { | |
"Hello" | |
"World" | |
} | |
print(greetCharCount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment