10 Sep 07:31 2013

## Lenses: Should I declare Getters?

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?

Thank you,

Charles Paul
_______________________________________________
10 Sep 17:04 2013

### Re: Lenses: Should I declare Getters?

On Tue, Sep 10, 2013 at 1:31 AM, Charlie Paul 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/>
_______________________________________________
10 Sep 17:06 2013

### Re: Lenses: Should I declare Getters?

On 09/10/2013 06:31 AM, Charlie Paul 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?

Generally, you don't provide 'Getter's because people can always lift a
function with "to" - as you said. This is mentioned in the lens FAQ at:

https://github.com/ekmett/lens/wiki/FAQ#wiki-using-getters

- ocharles

_______________________________________________