Append after index and update items after inserted item with lens

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Append after index and update items after inserted item with lens

Cody Goodman
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


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Append after index and update items after inserted item with lens

Li-yao Xia-2
The only item of index satisfying (> 2) in the list things is (A 3).

Li-yao


On 03/13/2018 02:44 AM, Cody Goodman wrote:

> 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
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Append after index and update items after inserted item with lens

Cody Goodman
Hi Li-Yao,

I made a mistake with my example. In my real world problem there are many elements after which need updated.

On Mar 13, 2018 5:50 AM, "Li-yao Xia" <[hidden email]> wrote:
The only item of index satisfying (> 2) in the list things is (A 3).

Li-yao


On 03/13/2018 02:44 AM, Cody Goodman wrote:
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



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.