Skip to content

Instantly share code, notes, and snippets.

@tikidunpon
Created March 14, 2017 14:43
Show Gist options
  • Select an option

  • Save tikidunpon/02edb487a6d47622cdc455099da762d0 to your computer and use it in GitHub Desktop.

Select an option

Save tikidunpon/02edb487a6d47622cdc455099da762d0 to your computer and use it in GitHub Desktop.
ArrayがEquatableに準拠してないね、という話の流れから生まれたArrayの比較が可能な関数です。 ありがとうございます。 @es_kumagai #read_swift #CodePiece
/// 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