Skip to content

Instantly share code, notes, and snippets.

@japgolly
Last active December 22, 2015 07:09
Show Gist options
  • Save japgolly/6436340 to your computer and use it in GitHub Desktop.
Save japgolly/6436340 to your computer and use it in GitHub Desktop.
val bandNameL = lensg[Band, String](b => n => b.copy(name = n), _.name)
val guitarTuningL = lensg[Guitar, String](g => t => g.copy(tuning = t), _.tuning)
// KeyedLens #1: Here Person is a key.
val guitarL = KeyedLens[Band, Person, Guitar](
b => p => newValue => b.copy(members = b.members + (p -> newValue)),
b => p => b.members(p))
// KeyedLens #2: Here Int is the key.
val stringGaugeL = KeyedLens[Guitar, Int, Double](
g => s => newValue => g.copy(gauges = g.gauges.updated(s-1, newValue)),
g => s => g.gauges(s-1))
// Compose a keyed and non-keyed lens
val guitaristsTuningL = guitarL >@=> guitarTuningL
// Compose 2 keyed lenses
val guitaristsGaugeL = guitarL >@=@> stringGaugeL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment