[Haskell-cafe] Append after index and update items after inserted item with lens
Cody Goodman
codygman.consulting at gmail.com
Tue Mar 13 13:56:10 UTC 2018
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" <lysxia at gmail.com> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180313/84463d27/attachment.html>
More information about the Haskell-Cafe
mailing list