I have a value:
my :: [(A, Either B [C])]
and I want to get [(A, C)] from it using lens. The result items are items from my that:
- the second item in tuples is
Right [C] - the list in this
Right [C]has at least 1 item, so the result get the first (head) item from it
I know how to get the [C] but I don't know how to get [(A, C)].
You want:
The idea is that
_Right . _headis a fold that selects aCfrom anEither B [C]. You can make it select an(A, C)from an(A, Either B [C]), usingsecondfromControl.Arrow. However, to do this, you need to reify the fold (i.e., turn it into a newtype with anArrowinstance) usingFoldand then unreify it withrunFold. The resulting fold specializes to:which selects either zero or one instances of
(A,C), and you can compose it witheachto fold over a traversable container, like a list:to select all of the
(A,C)from the container's elements.Sample code: