ingolemo / python-lenses

A python lens library for manipulating deeply nested immutable structures
GNU General Public License v3.0
310 stars 19 forks source link

nested _each does not work as expected #1

Closed mvcisback closed 8 years ago

mvcisback commented 8 years ago

The following example does not appear to work as expected:

In [17]: lens([[1,3], 4]).tuple_(lens()[0].each_(), lens()[1]).each_().get_all()
Out[17]: [4, 4]

Perhaps I'm misunderstanding, but shouldn't the result be [1,3,4] or something? It looks like it's implicitly using the + monoid

ingolemo commented 8 years ago

It's a limitation of TupleLens that you can't pass it a traversal since it does an implicit get_monoid as part of its current implementation. This may be fixable, Ill give it some thought.

In the mean time, you can work around this by extracting the each_ from out of the tuple_:

right = lens()[1].iso_(lambda a: [a], lambda a: a[0])
lens([[1,3], 4]).tuple_(lens()[0], right).each_().each_().get_all()

This works by constructing a tuple of two lists ([1, 3], [4]) which can then be eached over twice.

ingolemo commented 8 years ago

This is not fixable, at least not easily.

You would need to add some kind of set_all method and also a way to distinguish traversal results from ordinary ones. If I understand everything correctly, both of these would require inelegant hacks. I would rather keep the limitation and just improve documentation.