Created
January 12, 2016 14:27
-
-
Save mbrandonw/48ea2b3de9dc308907d9 to your computer and use it in GitHub Desktop.
This file contains 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
protocol LensType { | |
typealias Whole | |
typealias Part | |
var get: Whole -> Part { get } | |
var set: (Part, Whole) -> Whole { get } | |
} | |
struct Lens <A, B> : LensType { | |
typealias Whole = A | |
typealias Part = B | |
let get: A -> B | |
let set: (B, A) -> A | |
} | |
extension LensType where Whole == User, Part == Location { | |
var _name: Lens<User, String> { | |
return Lens( | |
get: { user in self.get(user).name }, | |
set: { (name, user) in User._location.set(Location._name.set(name, self.get(user)), user) } | |
) | |
} | |
} | |
extension LensType where Whole == Project, Part == Location { | |
var _name: Lens<Project, String> { | |
return Lens( | |
get: { project in self.get(project).name }, | |
set: { (name, project) in Project._location.set(Location._name.set(name, self.get(project)), project) } | |
) | |
} | |
} | |
extension LensType where Whole == Project, Part == User { | |
var _name: Lens<Project, String> { | |
return Lens( | |
get: { project in self.get(project).name }, | |
set: { (name, project) in Project._creator.set(User._name.set(name, self.get(project)), project) } | |
) | |
} | |
var _location: Lens<Project, Location> { | |
return Lens( | |
get: { project in self.get(project).location }, | |
set: { (location, project) in Project._creator.set(User._location.set(location, self.get(project)), project) } | |
) | |
} | |
} | |
let schema = Project._location._name *~ "LA" | |
|> Project._creator._name *~ "Joel Hodgson" | |
|> Project._creator._location._name *~ "New York" | |
mst3k |> schema | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment