[Haskell-cafe] Lenses: Should I declare Getters?

David Menendez dave at zednenem.com
Tue Sep 10 17:04:00 CEST 2013


On Tue, Sep 10, 2013 at 1:31 AM, Charlie Paul <charlieap at gmail.com> wrote:

> I've been looking through Edward Kmett's lens library, and I'm a bit
> befuddled about Getters. In my own code, why would I want to have something
> be a Getter instead of a plain function? As far as I can see, a plain
> function is simpler to use, and can be converted to a Getter with "to" if
> we want to use it as a Fold. Is there a situation where writing a Getter is
> superior than writing a function, then lifting it as needed?
>

 As I understand it, you'd be better off declaring a function and then
using "to".

Getters are a midpoint between a Lens and a Fold. With a lens, you can read
and write a single value in a larger structure. With a fold, you can read a
sequence of values in a larger structure.

The need for getters comes from to. "myLens . to f" cannot be a lens,
because modifying the value it points to would require an inverse for "f",
but if making it a fold would lose the property that it points to exactly
one value. So a getter lets you read a single value in a larger structure.

That being said, "view (myLens . to f)" is isomorphic to "f . view myLens".

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130910/66d368a2/attachment.htm>


More information about the Haskell-Cafe mailing list