3
votes

I'm a beginner of the lens library. I'm trying to extract a list from some data structure.

I wonder how to implement f that satisfies:

("a",[(0,'a'),(1,'b'),(2,'c')]) ^. _2 . f === [0,1,2]

With a struggle, I found an answer.

g :: Functor f => Getting a s a -> (forall b. Getting (f b) (f s) (f a))
g = to . fmap . view
f = g _1 

Is there any library function that corresponds to g? Otherwise, Is there any more elegant way to implement such f?

1

1 Answers

4
votes

I would go for

("a",[(0,'a'),(1,'b'),(2,'c')]) ^.. _2 . traverse . _1

(Note the ^.. instead of ^. because this is a traversal.)