Search code examples
haskelllenses

Filtering composite structures with Lens


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?


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.