Last active
October 10, 2015 10:53
-
-
Save dolphinsue319/78356e77e3413ca3445c to your computer and use it in GitHub Desktop.
Pattern matching 最後的結果
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
/* | |
第一個參數是一個會輸出 Bool 的函數,也就是 case 後面接的東西。 | |
第二個參數會被放入第一個函數型態的參數中,藉以輸出一個 Bool 值 | |
*/ | |
func ~=<T>(pattern: T->Bool, value: T) -> Bool { | |
return pattern(value) | |
} | |
func greaterThan<T: Comparable>(a:T)(b: T) -> Bool { | |
return b > a | |
} | |
func lessThan<T: Comparable>(a: T)(b: T) -> Bool { | |
return b < a | |
} | |
let x: Int = 1 | |
switch x { | |
case greaterThan(0): | |
print("positive") | |
case lessThan(0): | |
print("negative") | |
default: | |
print("0") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment