Question

I have a [(a, Maybe b)], and want to obtain a [(a, b)], with all pairs where the second element was Nothing filtered out. Is there a concise way to describe this operation using lens?

Était-ce utile?

La solution

How about something like

[('a',Just 1),('b',Nothing)]^..folded.aside _Just 

Using (^..) and folded from Control.Lens.Fold and aside and _Just from Control.Lens.Prism.

The key is aside, a handy function that builds a prism working on a pair from a prism working on a component of the pair.

Autres conseils

Notwithstanding the ingeniousity of the Lenses, the follwoing would probably be the mark for conciseness:

[ (a, b) | (a, Just b) <- list ]

(Not to speak of readability.)

mapMaybe sequenceA :: [(a, Maybe b)] -> [(a,b)]

You need to import Data.Traversable, Data.Maybe and have a Traversable ((,) a) instance. I leave figuring how does this work to the reader.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top