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?
问问题
1760 次
3 回答
13
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.
于 2013-10-25T14:22:44.137 回答
12
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.)
于 2013-10-25T15:01:29.203 回答
4
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.
于 2013-10-25T16:01:11.433 回答