Skip to content

Instantly share code, notes, and snippets.

@InsertNetan
Last active May 1, 2016 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InsertNetan/6ba68c063872ace05205f984b8300930 to your computer and use it in GitHub Desktop.
Save InsertNetan/6ba68c063872ace05205f984b8300930 to your computer and use it in GitHub Desktop.
struct Lens<A,B> {
let get: (A) -> B
let set: (B,A) -> A
}
extension Lens {
func compose<C>(other: Lens<B, C>) -> Lens<A, C> {
return Lens<A, C>(
get: { whole in
let part = self.get(whole)
let subpart = other.get(part)
return subpart
},
set: { (newSubpart, whole) in
let part = self.get(whole)
let newPart = other.set(newSubpart, part)
let newWhole = self.set(newPart, whole)
return newWhole
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment