Search code examples
haskellidiomshaskell-lenslenses

Eliminating `flip` in Haskell lens


I'm trying to get the hang of lenses. Is there a more idiomatic way to write the following? (placeholders preceded by underscores)

flip (set _lens) _a . fmap _f

To me, the use of flip seems to suggest non-idiomatic code. Is there a better combinator that avoids flip in this situation? Is there a more lens-like way of integrating the fmap?


Solution

  • In this case, you might want to consider writing it pointed

    \x -> _a & _lens .~ fmap _f x
    

    which feels much more idiomatic to me.

    If you really want it pointfree without flip, you can convert the above to pointfree:

    (_a &) . set _lens . fmap _f
    

    (Although technically, since & is equivalent to flip ($), you're really just hiding the flip.)