0

I'm trying to write a function with the following type-signature using lenses, but staring blankly at filtered is apparently not working!

modifyElem
  :: (a -> Bool)      -- predicate function
  -> (a -> a)         -- modification to apply
  -> [a]
  -> [a]

If possible, this could also be generalised further to the following:

modifyElem
  :: (Foldable t)     -- or (Traversable t)
  -> (a -> Bool)
  -> (a -> a)
  -> t a
  -> t a
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60

1 Answers1

3

Try this:

modifyElem :: Traversable t => (a -> Bool) -> (a -> a) -> t a -> t a
modifyElem predicate transform target =
  target & traverse . filtered predicate %~ transform
rampion
  • 87,131
  • 49
  • 199
  • 315