Search code examples
haskellhaskell-lenslenses

How to remove an item from a list by index using the lens library?


I can view the, say, 4th item in a list using a lens like this:

preview (ix 3) myList

Is there something that could replace "preview" in order to remove the fourth item from the list instead of viewing it? The return list should be the same as the original list say the 4th item would be deleted.

Or perhaps there may be a way of doing this using the filtered function?


Solution

  • Sounds like you want to use ifiltered:

    toListOf (folded . ifiltered (\i _ -> i /= 3)) $ myList
    
    -- or
    
    myList ^.. folded . ifiltered (\i _ -> i /= 3))