Hacker News new | ask | show | jobs
by ozataman 4711 days ago
For example: I personally prefer the standard form here, as (to me) it reads a bit more like English:

over (location.x) (+ 10) $ player1

"over location's x, add 10"

1 comments

There's also the

   player1 & location.x .~ (+10)
format which has a nice scansion.
It seems like there should be a way to define player1 as its own (root) lens so that one is simply writing player1.location.x .~ (+10), or so. Are we simply not doing this because it makes reading the data structure rather difficult?
That can kind of be done using `Context`s, but those are really just implementation details and should rarely be used.

Broadly, you wouldn't want to do that because it implies mutation. There's not much value in `player1.location.x %~ (+10)` because it can't modify player1... just return a modified (player1'new).

Of course, stateful mutation is just a monadic effect, so you can wrap it up into a State monad, which Lens already has shortcuts for

    do
      location.x += 10
      location.y += 10
which has type like `MonadState ThingWithLocation m => m ()` and we can then apply it to whatever `player` we want as a pure function.