Skip to content

Instantly share code, notes, and snippets.

@KentaKudo
Last active May 22, 2017 15:10
Show Gist options
  • Save KentaKudo/8843392ce13dbfeb66f00e08625a538b to your computer and use it in GitHub Desktop.
Save KentaKudo/8843392ce13dbfeb66f00e08625a538b to your computer and use it in GitHub Desktop.
Convert the order of array and needle
// standard
[1,2,3,4,5,6,7].contains(3)
// alternative
protocol A {
func `is`(in haystack: [Self]) -> Bool
func `is`(in haystack: Self...) -> Bool
}
extension A where Self: Comparable {
func `is`(in haystack: [Self]) -> Bool { return haystack.contains(self) }
func `is`(in haystack: Self...) -> Bool { return self.is(in: haystack) }
}
extension Int: A {}
3.is(in: [1,2,3,4,5,6,7])
3.is(in: 1,2,3,4,5,6,7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment