[Haskell-cafe] Append after index and update items after inserted item with lens

Cody Goodman codygman.consulting at gmail.com
Tue Mar 13 06:44:25 UTC 2018


Hello,

I'm trying to insert a new elemnt into a list after a given index and
increment the orderId accordingly with the lens library.

I'm a bit confused at why this code isn't working:

```
import Control.Lens

data A = A { _customOrderId :: Int } deriving Show

things = [ A {_customOrderId = 0}
         , A {_customOrderId = 1}
         , A {_customOrderId = 2}
         , A {_customOrderId = 3}
         ]

main = print $ things & ( (_drop 2 %~ ((A 22)<|)) . ((traversed . indices
(> 2) . customerOrderIdLens) %~ (+ 1))  )
```
The first portion works to insert where desired, but the second portion
`((traversed . indices (> 2) . customerOrderIdLens) %~ (+ 1))` only
increments the order id in indices 4.

Any idea why this is happening?

Thanks,

Cody Goodman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180313/642b03c6/attachment.html>


More information about the Haskell-Cafe mailing list