Skip to content

Instantly share code, notes, and snippets.

@spnkr
Forked from JadenGeller/Swift Unless_When.swift
Last active May 21, 2019 18:53
Show Gist options
  • Save spnkr/b7da6dcd61cf4aa717f4bfa34d3ac8b1 to your computer and use it in GitHub Desktop.
Save spnkr/b7da6dcd61cf4aa717f4bfa34d3ac8b1 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