Python's enumerate on lists can be written as zip [0..]. I looked at Control.Lens.Traversal and Control.Lens.Indexed, but I couldn't figure out how to use lenses to generalize this to any reasonable container (I hesitate to say "Traversable").
I'm guessing itraverse or itraverseOf is key.
If you're using a container that is an instance of FunctorWithIndex then you can simply use imap (,):
> imap (,) "abc"
[(0,'a'),(1,'b'),(2,'c')]
But if the index isn't the position this won't work:
> let m = Map.fromList [('a', "foo"), ('b', "bar"), ('c', "foobar")])
> imap (,) m
fromList [('a',('a',"foo")),('b',('b',"bar")),('c',('c',"foobar"))]
Instead you can use traversed, which is an indexed traversal where the index is the order the elements appear. This can be used for anything that's Traversable. Instead of imap use iover traversed (which is the same as imapOf but that's been deprecated):
> iover traversed (,) "abc"
[(0,'a'),(1,'b'),(2,'c')]
> iover traversed (,) m
fromList [('a',(0,"foo")),('b',(1,"bar")),('c',(2,"foobar"))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With