Created
August 25, 2017 11:38
-
-
Save tonyarnold/5c5205a82aa380355e4fb77ba49d3837 to your computer and use it in GitHub Desktop.
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
// | |
// 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