I wanted to try lenses and the Monocle library seemed (from my noobish perspective) good with all those fancy boilerplate-less @Lenses
. Unfortunately I found out there are little to non learning materials for beginners (I know basics of FP in vanilla Scala, no Scalaz). Official tutorial lacks easy examples (and/or their results) and mixes in quite complex Scalaz library. One would assume that such trivial task like accessing a Map would be covered on a first page.
I have following snippet:
@Lenses case class House(presentsDelivered: Int)
type Houses = Map[(Int, Int), House]
@Lenses case class Town(houses: Houses)
@Lenses case class Santa(x: Int, y: Int)
@Lenses case class World(santa: Santa, town: Town)
I saw at
and index
, but no simple examples (just some weird [magic for me] answer with applyOptional
which required boilerplate). I want to update the map - houses
in Town
. I was trying something in this spirit:
(World.town ^|-> Town.houses ^|-> index((x, y)) ^|-> House.presentsDelivered)
.modify { _ + 1 }(world)
Which is syntactically wrong, but I think it's apparent what I wanted to do (modify presentsDelivered
of House
at specified x, y
coordinates). So my question is, how to modify the index
part to access the map?
Any help, clue or noob-friendly learning materials tips are welcome.