isContained(in array:) extension of Equatable

Ooh, a fellow Ukelele-ist!

On my custom layout it’s ⌥⇧L to enter Logic mode, then P to type ∈ (not sure why I put it there exactly. Probably so it’s next to ⋃ (U), ⋂ (I), and ∅ (O). E was already taken by ∃.)

• • •

I think the larger issue in this thread has to do with inversion of method calls during a functional-style chain. Until we have a good story there, I don’t think it’s worth special-casing this sort of thing.

For those who want it, the generic implementation of the OP’s request is trivially composable from existing features:

extension Equatable {
  func isIn<S: Sequence>(_ s: S) -> Bool where S.Element == Self {
    return s.contains(self)
  }
}

And the variadic version too:

extension Equatable {
  func isOneOf(_ s: Self...) -> Bool {
    return s.contains(self)
  }
}
2 Likes