autotaker
autotaker

Reputation: 31

How to manipulate lists with lens in Haskell

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?

Upvotes: 3

Views: 85

Answers (1)

kosmikus
kosmikus

Reputation: 19637

I would go for

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

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

Upvotes: 4

Related Questions