Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active May 21, 2019 16:11
Show Gist options
  • Save JadenGeller/2828e1f7458c8b41a7b0 to your computer and use it in GitHub Desktop.
Save JadenGeller/2828e1f7458c8b41a7b0 to your computer and use it in GitHub Desktop.
Swift Unless/When
// Basically, these are if and else statements respectively
// without the opposite clause
func when(test: @autoclosure () -> Bool, action: () -> ()) {
if test() { action() }
}
func unless(test: @autoclosure () -> Bool, action: () -> ()) {
if !test() { action() }
}
// Examples
when(3 == 5){
println("three equals five") // Will print!
}
unless(3 == 5){
println("three does not equal five") // Will never print.
}
when(1 == 1){
println("one equals one") // Will print!
}
unless(1 == 1){
println("one does not equal one") // Will never print.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment