Last active
June 26, 2017 20:02
-
-
Save djtech42/9e41b8254d86b8df586653d1bf1fe30b to your computer and use it in GitHub Desktop.
Ternary Function for Swift for more readability
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
var value = 1005 | |
print(ternary(if: value > 1000, then: "big", else: "small")) // OUTPUT: big | |
value = 5 | |
print(ternary(if: value > 1000, then: "big", else: "small")) // OUTPUT: small |
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
func ternary<T>(if condition: Bool, then value: T, else otherValue: T) -> T { | |
return condition ? value : otherValue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment