Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:25
Show Gist options
  • Save oisdk/12d302c72a1bd5b004f1 to your computer and use it in GitHub Desktop.
Save oisdk/12d302c72a1bd5b004f1 to your computer and use it in GitHub Desktop.
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