Skip to content

Instantly share code, notes, and snippets.

@ennioma
Created February 22, 2018 12: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 ennioma/913c70a861d9174f79751b3b770f2a96 to your computer and use it in GitHub Desktop.
Save ennioma/913c70a861d9174f79751b3b770f2a96 to your computer and use it in GitHub Desktop.
Lenses Composition
precedencegroup ComposePrecedence {
associativity: left
}
infix operator >>> : ComposePrecedence
func >>> <Container, View, Subview>(lhs: Lens<Container, View>, rhs: Lens<View, Subview>) -> Lens<Container, Subview> {
return Lens(
get: { container in
let view = lhs.get(container)
let subview = rhs.get(view)
return subview
},
set: { (container, subview) in
let initialView = lhs.get(container)
let updatedView = rhs.set(initialView, subview)
return lhs.set(container, updatedView)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment