Last active
August 29, 2015 14:25
-
-
Save oisdk/12d302c72a1bd5b004f1 to your computer and use it in GitHub Desktop.
This file contains 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
struct _Empty<T>: ArrayLiteralConvertible { | |
init(arrayLiteral: T...) { | |
assert(arrayLiteral.isEmpty) | |
} | |
} | |
func ~= <C : CollectionType>(lhs: _Empty<C.Generator.Element>, rhs: C) -> Bool { | |
return rhs.isEmpty | |
} | |
func ~= <T>(lhs: T -> Bool, rhs: T) -> Bool { | |
return lhs(rhs) | |
} | |
let emptyAr: [Int] = [] | |
switch emptyAr { | |
case []: print("empty") // "empty" | |
default: print("not") | |
} | |
let nonEmptyAr = [1, 2, 3] | |
switch nonEmptyAr { | |
case []: print("empty") | |
default: print("not") // "not" | |
} | |
let isEven = {(n: Int) -> Bool in n % 2 == 0} | |
switch 2 { | |
case isEven: print("even") // "even" | |
default: print("odd") | |
} | |
switch 3 { | |
case isEven: print("even") | |
default: print("odd") // "odd" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment