6
只許成功試想一下,我有以下列表:應用兩個摺痕或getter和當兩個成功
lst :: [(Bool, Maybe Integer)]
lst = [(True, Just 3), (True, Nothing), (False, Just 12)]
使用鏡頭庫,我想提取的元組的元素,但我只希望它成功當第二個元素是Just
。我想要一些光,split
認爲是這樣的:
> lst ^.. folded.split (_1.to not) (_2._Just)
[(False, 3), (True, 12)]
我可以實現自己split
像這樣:
split :: Getting (First a) s a -> Getting (First b) s b -> Fold s (a, b)
split a b = folding (\x -> (,) <$> (x ^? a) <*> (x ^? b))
...這似乎工作。不過,這似乎是我必須重新發明輪子。鏡頭庫已經提供了一種以同樣好的方式實現這一點的東西嗎?