我正试着用镜头写一个具有以下类型签名的函数,但是茫然地盯着 filtered 显然不行!
filtered
modifyElem :: (a -> Bool) -- predicate function -> (a -> a) -- modification to apply -> [a] -> [a]
如果可能的话,还可以进一步概括如下:
modifyElem :: (Foldable t) -- or (Traversable t) -> (a -> Bool) -> (a -> a) -> t a -> t a
试试这个:
modifyElem :: Traversable t => (a -> Bool) -> (a -> a) -> t a -> t a modifyElem predicate transform target = target & traverse . filtered predicate %~ transform