by IanCal · 13y ago · 0 comments
location.x `over` (+10) $ player1
Here, `over` is not a string. This is not stringly typed programming. It translates to over (location.x) (+ 10) $ player1
It's used to take a function that would normally be function_name argument1 argument2
and allow you to call it like so argument1 `function_name` argument2
so plus 1 2 = 1 `plus` 2
There's nothing special you need to do, and it's purely a style thing. Sometimes it's easier to read.Edit - fix from DanWaterworth, the . in location.x is (if I understand the problem correctly) function composition and therefore location.x is location . x, so my re-ordering possibly changed things more than I thought. The principle is the same, but I've wrapped location.x in parentheses, just to be sure. I'm sure someone more well versed in haskell can correct me on this :)
over location.x (+ 10) $ player1
Would be read as: (over location) . (x (+ 10)) $ player1
It looks like what you want is: over (location . x) (+ 10) $ player1over (location.x) (+ 10) $ player1
"over location's x, add 10"
player1 & location.x .~ (+10)
format which has a nice scansion.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.