Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apply lenses to struct
extension Actor {
struct Lenses {
static let name = Lens<Actor, String>(
get: {$0.name},
set: {(me, value) in Actor(name: value, surname: me.surname) }
)
static let surname = Lens<Actor, String>(
get: {$0.surname},
set: {(me, value) in Actor(name: me.name, surname: value) }
)
}
}
extension Movie {
struct Lenses {
static let mainActor = Lens<Movie, Actor?>(get: { movie in
let actor: Actor? = movie.actors.first
return actor
}, set: { me, actor -> Movie in
guard let actor = actor else { return me }
return Movie(title: me.title, year: me.year, actors: [actor] + me.actors)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment