Last active
September 19, 2023 13:12
-
-
Save bill1m/7b83005bd54410b11c6723524ac5cc87 to your computer and use it in GitHub Desktop.
OptionSet CaseIterable enum
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
/// Day rawValues used in WeekdaySet. Day proper names capitalized. | |
enum Day: UInt8, CaseIterable { | |
case Sunday = 0b00000001 | |
case Monday = 0b00000010 | |
case Tuesday = 0b00000100 | |
case Wednesday = 0b00001000 | |
case Thursday = 0b00010000 | |
case Friday = 0b00100000 | |
case Saturday = 0b01000000 | |
var description: String { | |
return "\(self)" | |
} | |
} | |
/// Seven days of the week represented with binary options. | |
struct WeekdaySet: OptionSet { | |
/// WeekdaySet initialized with Day (not with Weekday) | |
static let Sunday = WeekdaySet(.Sunday) | |
static let Monday = WeekdaySet(.Monday) | |
static let Tuesday = WeekdaySet(.Tuesday) | |
static let Wednesday = WeekdaySet(.Wednesday) | |
static let Thursday = WeekdaySet(.Thursday) | |
static let Friday = WeekdaySet(.Friday) | |
static let Saturday = WeekdaySet(.Saturday) | |
/// WeekdaySet initialized with Weekday (not with Day) | |
static let all: WeekdaySet = [.Sunday, .Monday, .Tuesday, .Wednesday, .Thursday, .Friday, .Saturday] | |
static let weekend: WeekdaySet = [.Saturday, .Sunday] | |
static let midweek: WeekdaySet = [.Tuesday, .Wednesday, .Thursday] | |
static let humpday: WeekdaySet = .Wednesday | |
/// OptionSet conformance | |
let rawValue: UInt8 | |
init(rawValue: UInt8) { | |
self.rawValue = rawValue | |
} | |
/// Init using the enum Day | |
init (_ day: Day) { | |
self.rawValue = day.rawValue | |
} | |
} | |
extension WeekdaySet: CaseIterable { | |
static var allCases: [WeekdaySet] { | |
[ | |
.Sunday, | |
.Monday, | |
.Tuesday, | |
.Wednesday, | |
.Thursday, | |
.Friday, | |
.Saturday, | |
] | |
} | |
/// Computed instance property to filter static allCases | |
var array: [WeekdaySet] { | |
get { | |
return WeekdaySet.allCases.filter { self.contains($0) } | |
} | |
} | |
} | |
extension WeekdaySet: Sequence { | |
typealias Iterator = AnyIterator<WeekdaySet> | |
func makeIterator() -> Iterator { | |
var iterator = array.makeIterator() | |
return AnyIterator { | |
return iterator.next() | |
} | |
} | |
} | |
extension WeekdaySet: CustomStringConvertible { | |
var description: String { | |
if self.array.count < 2 { | |
return Day(rawValue: self.rawValue)?.description ?? "" | |
} | |
var results: [String] = [] | |
for weekday in self.array { | |
results.append(Day(rawValue: weekday.rawValue)?.description ?? "") | |
} | |
return String(describing: results) | |
} | |
} | |
/// A example set of weekdays | |
let customSet: WeekdaySet = [.Monday, .Tuesday] | |
/// Example usages: | |
print("Does the example set contain humpday?", customSet.contains(.humpday)) | |
for weekday in customSet { | |
print("Is \(weekday) midweek?", WeekdaySet.midweek.contains(weekday)) | |
} | |
print("Thursday:", WeekdaySet.Thursday) | |
print("Weekend names:", WeekdaySet.weekend) | |
print("All names", WeekdaySet.all) | |
// Printed results: | |
// Does the example set contain humpday? false | |
// Is Monday midweek? false | |
// Is Tuesday midweek? true | |
// Thursday: Thursday | |
// Weekend names: ["Sunday", "Saturday"] | |
// All names ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment