Last active
May 1, 2016 07:20
-
-
Save InsertNetan/6ba68c063872ace05205f984b8300930 to your computer and use it in GitHub Desktop.
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
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