-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"at" example doesn't work #36
Comments
Turns out I had containers-0.5.0.0 on the system which messed things up. Unregistering it solved the problem. |
You appear to have multiple versions of |
Yes, I figured the containers version issue following your response on IRC. I created https://gist.github.com/3680166 - it contains two files. One contains utilities which don't currently exist in the lens package, and
The second file contains a suite of HUnit tests which demonstrate using the My motivation in creating this second file was basically capturing my Speaking of my understanding, a quick question - inside the state monad, At any rate, many thanks for this package, it certainly looks like this is Thanks,
On Sat, Sep 8, 2012 at 5:55 PM, Edward A. Kmett notifications@github.comwrote:
|
I'll definitely add the .:-like operator. probably as $>> given the precedence it should have and the way it reads when chained. The only question is really what module it should live in. I've already implemented the ++~, etc. operators and they definitely will be in 2.7. That said, I'm not entirely sold on adding the << variants of the combinators given that you can implement them with the existing %%~ combinators and the semiotics of < was simply that it was returning the value to the left. Perhaps if I can come up with an appropriate notation that doesn't break the meaning of the existing combinators. Putting the < on the right hand side of the operator might have the desired notational effect. e.g. ++<~ but it risks muddling the already enormous set of symbols we're exporting by forcing the user to mentally split the operator in a different place. =/ The problem is when you go to implement a 'monadic' %%= what should happen to the intermediate edits? you have monadic effects, those probably want to access or edit part of your state as well, so the order of events and meaning when you overwrite portions of the existing state in the current monad are somewhat ill posed. There is a <~ operator that can assign the result of a monadic action to a lens though. This limited scenario works because the edit takes place after the action completes so there is a clear phase separation. |
I added |
@orenbenkiki: I split off adding |>, <<++~, and the hunit test harness into separate issues as #37, #38 and #39 respectively. |
Thanks for integrating the code! Yes, using |> is the same as F#, I should have thought of that. It is I agree making sense into the operator mnemonics is important, especially I can see how it would be consistent to say <+= and +<=, and I briefly Whether this is important enough a use case to warrant an operator, well...
field .= oldValue I suppose I should just wrap this in a generic function along the lines of:
value One would need to add the withValue_ and withModified_ variants that always As for the monadic modification application - intuitively, one would expect I'm not certain what would be the ideal notation for a monadic %= / %%= I could whip up some patch code for |%=, |%%=, withValue and withModified Thanks again, Oren Ben-Kiki On Sun, Sep 9, 2012 at 3:27 AM, Edward A. Kmett notifications@github.comwrote:
|
The problem is that there isn't a good way to overload the monadic traversal of your state so it edits one location at a time. You don't wind up 'repacking' the resulting state into a bundled up state until you're finished traversing all of the locations. =/ Also, intermediate actions may change the number of places that would be valid targets. Consider what happens when you have a Traversal of a list that comprises your state. There is no valid answer for how to tackle this one. The monadic action might remove entries, replace the list entirely, etc. The reason this combinator doesn't exist is because it can't. ;) It can exist in a limited lens-only form, but I have to admit that isn't a very satisfying combinator, because even that one doesn't necessarily have sensible laws for it. It can legally move the target of the lens, so reading, updating state and writing back can well write back to an entirely different location than was read from. Consider a lens into the last location of a list. I'm sorry I'm such a stickler for laws, but its the only way to keep the package sane. Regarding the <<-combinators, I pushed the reply over to #39, to help me keep track of what changes are still open for consideration. ;) |
I think I understand the concern but it seems to me this applies only to That is, I am hard pressed to think of a case where using the simple Perhaps I am missing something...? Oren. On Sun, Sep 9, 2012 at 2:28 PM, Edward A. Kmett notifications@github.comwrote:
|
Consider |
These issues manifest themselves in other languages too. Many languages like c++ have iterators, but inserting into the container while iterating may or may not be legal, and leads to all sorts of confusing semantics and places where elements may be missed in the traversal. |
Ouch. You are right, doing "list . _last |%= functionThatAppendsToList" would have ill-defined semantics. It is a pity to give up this very useful ability because of this edge case... but if we'd rather be safe than sorry, this kills "|%=" and "|%%=". If we'd rather maximize usability, I'm not so sure (people could be warned off this edge case). I guess leaving it out for now would be safest, this can always be re-visited in the future if there is sufficient demand for it. |
Going to close out the issue and leave it as a record of the discussion. |
Trying the example from the source comments:
BTW, what is the equivalent of "at" for accessing the n-th element of a list? I am sorely missing a "cheat sheet" that lists the common cases of using lenses, instead of hunting them across all the source files... In fact I was creating an HUnit test file that does nothing but list all these common cases, when I run into the above.
The text was updated successfully, but these errors were encountered: