Skip to content

Instantly share code, notes, and snippets.

@ennioma
Last active February 22, 2018 11:42
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/2933e66ff5c7ba66aead7969f619f827 to your computer and use it in GitHub Desktop.
Save ennioma/2933e66ff5c7ba66aead7969f619f827 to your computer and use it in GitHub Desktop.
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