Skip to content

Instantly share code, notes, and snippets.

@brentdax brentdax/results.txt
Created Mar 14, 2016

Embed
What would you like to do?
How many lenses would we need for different policies on accessor `mutating` and `throws` attributes?
20 variants for independent mutating, independent throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get throws set
get throws set throws
get mutating set
get mutating set throws
get throws mutating set
get throws mutating set throws
mutating get set
mutating get set throws
mutating get throws set
mutating get throws set throws
mutating get mutating set
mutating get mutating set throws
mutating get throws mutating set
mutating get throws mutating set throws
16 variants for independent mutating, dependent throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get throws set throws
get mutating set
get mutating set throws
get throws mutating set throws
mutating get set
mutating get set throws
mutating get throws set throws
mutating get mutating set
mutating get mutating set throws
mutating get throws mutating set throws
16 variants for dependent mutating, independent throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get throws set
get throws set throws
get mutating set
get mutating set throws
get throws mutating set
get throws mutating set throws
mutating get mutating set
mutating get mutating set throws
mutating get throws mutating set
mutating get throws mutating set throws
13 variants for dependent mutating, dependent throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get throws set throws
get mutating set
get mutating set throws
get throws mutating set throws
mutating get mutating set
mutating get mutating set throws
mutating get throws mutating set throws
12 variants for independent mutating, setter-only throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get mutating set
get mutating set throws
mutating get set
mutating get set throws
mutating get mutating set
mutating get mutating set throws
10 variants for dependent mutating, setter-only throws:
get
get throws
mutating get
mutating get throws
get set
get set throws
get mutating set
get mutating set throws
mutating get mutating set
mutating get mutating set throws
protocol ValuesEnumerable {
static var allValues: [Self] { get }
}
extension Bool: ValuesEnumerable {
static let allValues = [false, true]
}
protocol AccessorPairAttribute: ValuesEnumerable {
static var name: String { get }
var getter: Bool { get }
var setter: Bool { get }
}
struct Independent: AccessorPairAttribute {
static var name = "independent"
let getter: Bool
let setter: Bool
static let allValues = Bool.allValues.flatMap { getter in
Bool.allValues.map { setter in
Independent(getter: getter, setter: setter)
}
}
}
enum Dependent: AccessorPairAttribute {
static var name = "dependent"
case neither
case setterOnly
case both
static let allValues = [neither, setterOnly, both]
var getter: Bool {
return self == both
}
var setter: Bool {
return self != neither
}
}
enum SetterOnly: AccessorPairAttribute {
static var name = "setter-only"
case neither
case setterOnly
static let allValues = [ neither, setterOnly ]
var getter: Bool {
return false
}
var setter: Bool {
return self != neither
}
}
enum LensVariant<MutatesAttribute: AccessorPairAttribute, ThrowingAttribute: AccessorPairAttribute>: ValuesEnumerable {
case ReadOnly (mutates: Bool, throwing: Bool)
case ReadWrite (mutates: MutatesAttribute, throwing: AccessorPairAttribute)
static var allValues: [LensVariant] {
return Bool.allValues.flatMap { mutates in
Bool.allValues.map { throwing in
ReadOnly(mutates: mutates, throwing: throwing)
}
} +
MutatesAttribute.allValues.flatMap { mutates in
ThrowingAttribute.allValues.map { throwing in
ReadWrite(mutates: mutates, throwing: throwing)
}
}
}
}
struct Declaration: CustomStringConvertible {
var name: String
var mutates: Bool
var throwing: Bool
var description: String {
var str = name
if mutates {
str = "mutating \(str)"
}
if throwing {
str = "\(str) throws"
}
return str
}
}
extension LensVariant: CustomStringConvertible {
var declarations: [Declaration] {
switch self {
case let .ReadOnly(mutates, throwing):
return [
Declaration(name: "get", mutates: mutates, throwing: throwing)
]
case let .ReadWrite(mutates, throwing):
return [
Declaration(name: "get", mutates: mutates.getter, throwing: throwing.getter),
Declaration(name: "set", mutates: mutates.setter, throwing: throwing.setter)
]
}
}
var description: String {
return declarations.map { $0.description }.joinWithSeparator(" ")
}
}
func printLensVariants<MutatesAttribute: AccessorPairAttribute, ThrowingAttribute: AccessorPairAttribute>(mutates mutates: MutatesAttribute.Type, throwing: ThrowingAttribute.Type) {
let variants = LensVariant<MutatesAttribute, ThrowingAttribute>.allValues
print("\(variants.count) variants for \(mutates.name) mutating, \(throwing.name) throws:")
for variant in variants {
print("\t\(variant)")
}
print("")
}
printLensVariants(mutates: Independent.self, throwing: Independent.self)
printLensVariants(mutates: Independent.self, throwing: Dependent.self)
printLensVariants(mutates: Dependent.self, throwing: Independent.self)
printLensVariants(mutates: Dependent.self, throwing: Dependent.self)
printLensVariants(mutates: Independent.self, throwing: SetterOnly.self)
printLensVariants(mutates: Dependent.self, throwing: SetterOnly.self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.