How does one use an IndexedTraversal in Control.Lens to perform an index-aware action for each element?

StackOverflow https://stackoverflow.com/questions/19695864

  •  02-07-2022
  •  | 
  •  

Pergunta

I have an IndexedTraversal (from the Control.Lens package) and I would like to apply an index-aware monadic action to each element in it. Unfortunately, all of the convenient ways that I see of doing something like this --- such as ^! combined with act function --- seem to ignore the index with each element. Is there a nice way to run an action for every element (and its index) in an indexed traversal?

Foi útil?

Solução

Does imapMOf work? You would use it as imapMOf someIndexedTraversal actionWithIndex dataStructure I think.

If you just need to perform an action, there's also imapMOf_ in Control.Lens.Fold.

I haven't used indexed traversals much, but I find the API a bit confusing. Most of the time I use lenses with either ^. or ^!, but for indexed traversals it seems the usual way is to use one of the special index-aware functions, which seems a bit different.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top