Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Created August 25, 2017 11:38
Show Gist options
  • Save tonyarnold/5c5205a82aa380355e4fb77ba49d3837 to your computer and use it in GitHub Desktop.
Save tonyarnold/5c5205a82aa380355e4fb77ba49d3837 to your computer and use it in GitHub Desktop.
//
// Copyright © 2017 Tony Arnold. All rights reserved.
enum CompoundValue<Element: Hashable> {
case distinct(Element)
case indistinct(Set<Element>)
case empty
var distinctValue: Element? {
switch self {
case .distinct(let element):
return element
default:
return nil
}
}
var indistinctValues: Set<Element> {
switch self {
case .indistinct(let elements):
return elements
default:
return []
}
}
}
extension Set {
var compoundValue: CompoundValue<Element> {
if count > 1 {
return .indistinct(self)
} else if let value = first {
return .distinct(value)
} else {
return .empty
}
}
}
extension Collection where Element: Hashable {
var compoundValue: CompoundValue<Element> {
return Set(self).compoundValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment