Last active
December 22, 2015 07:09
-
-
Save japgolly/6436340 to your computer and use it in GitHub Desktop.
This file contains 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
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