Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created January 12, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbrandonw/48ea2b3de9dc308907d9 to your computer and use it in GitHub Desktop.
Save mbrandonw/48ea2b3de9dc308907d9 to your computer and use it in GitHub Desktop.
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