Takayuki Muranushi | 3 Jan 10:48 2013
Picon

Lens, with ability to create a data-field when it's missing

Dear All, I really am enjoying the lens (it is tough to learn, though :)

Recently, I've been trying to implement a dynamic object --- a
collection of methods that you can update at run-time --- using lens
[1][2][3]. After several attempts, I think I have located the center
of my problem:

Lens with the ability "to create a data-field when it's missing." My
question: is it possible?

There are many excellent examples of partial lenses in the lens
library, such as '_head' from Data.List.Lens 'dynamic' from
Data.Dynamic.Lens . To me, they seem to share the following set of
common behavior (allow me to use inaccurate terms...)

- set l a s : if the field is present, insert 'a'; otherwise, do nothing.
- over l f s : if the field is present, map 'f' over it. otherwise, do nothing.
- preview l s : if the field is present, return 'Just a'. otherwise,
return 'Nothing' .

Instead, I want the following set of behaviors:

- set l a s : if the field is present, insert 'a'; otherwise, *create
the field* and insert 'a'.
- over l f s : if the field is present, map 'f' over it. otherwise, do nothing.
- preview l s : if the field is present, return 'Just a'. otherwise,
return 'Nothing' .

For example, the current behavior of _head is
    >>> [] & _head .~ 1
(Continue reading)

Takayuki Muranushi | 10 Jan 16:11 2013
Picon

Re: Lens, with ability to create a data-field when it's missing

Dear everyone,

With help of many, I found ways to implement most of the features I
have wanted, and I can release the new dynamic-object package.
http://hackage.haskell.org/package/dynamic-object

Thank you in advance for your supports,

Takayuki

2013/1/3 Takayuki Muranushi <muranushi <at> gmail.com>:
> Dear All, I really am enjoying the lens (it is tough to learn, though :)
>
> Recently, I've been trying to implement a dynamic object --- a
> collection of methods that you can update at run-time --- using lens
> [1][2][3]. After several attempts, I think I have located the center
> of my problem:
>
> Lens with the ability "to create a data-field when it's missing." My
> question: is it possible?
>
> There are many excellent examples of partial lenses in the lens
> library, such as '_head' from Data.List.Lens 'dynamic' from
> Data.Dynamic.Lens . To me, they seem to share the following set of
> common behavior (allow me to use inaccurate terms...)
>
> - set l a s : if the field is present, insert 'a'; otherwise, do nothing.
> - over l f s : if the field is present, map 'f' over it. otherwise, do nothing.
> - preview l s : if the field is present, return 'Just a'. otherwise,
> return 'Nothing' .
(Continue reading)


Gmane