Search code examples
haskellhaskell-lens

What is the equivalent to `span` using `lens`?


Let's say I have

data SumType = A | B | C

list = [A, B, B, A, C, A]

(as, rest) = span (\case A -> True; _ -> False) list

Assuming I have the prism _A, is there a way to write this more succinctly using lens?


Solution

  • You could say

    (as, rest) = break (isn't _A) list
    

    isn't is defined in Control.Lens.Prism. Unfortunately there doesn't seem to be a corresponding function is' p = not . isn't p, although

    (as, rest) = span (isRight . matching _A) list
    

    would also work.