Search code examples
haskellhaskell-lens

Modify all even values with a lens


Suppose I have an array like this: [1, 2, 3, 4]

How would I modify only the even values using the lens package? I'm looking for something like:

filterLens even (+10) $ [1, 2, 3, 4]
=> [1, 12, 3, 14]

Solution

  • Prelude Control.Lens> [1, 2, 3, 4] & each . filtered even %~ (+ 10)
    [1, 12, 3, 14]
    

    To find functions like this, you can instruct hoogle to search the lens package by putting "+lens" into the search field. In this case: http://www.haskell.org/hoogle/?hoogle=%2Blens+filter