Created
March 14, 2017 14:43
-
-
Save tikidunpon/02edb487a6d47622cdc455099da762d0 to your computer and use it in GitHub Desktop.
ArrayがEquatableに準拠してないね、という話の流れから生まれたArrayの比較が可能な関数です。 ありがとうございます。 @es_kumagai #read_swift #CodePiece
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
| /// ArrayがEquatableに準拠してないね、という話の流れから生まれたArrayの比較が可能な関数 | |
| func isEqual<T: Equatable>(_ x: Array<T>, _ y: Array<T>) -> Bool { | |
| guard x.count == y.count else { | |
| return false | |
| } | |
| return zip(x,y).reduce(true) { result, elements in | |
| result && (elements.0 == elements.1) | |
| } | |
| } | |
| isEqual([1], [1]) | |
| //isEqual([1], ["1"]) //コンパイルエラー |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment