3 ms·
Can you describe more about how this works, specifically how it's an improvement over __setitem__ and __setattr__? I'm interested in how various languages hand
by zwegner 12y ago
Can you describe more about how this works, specifically how it's an improvement over __setitem__ and __setattr__?
I'm interested in how various languages handle this, mainly so I can steal ideas for my project Mutagen, a purely functional Pythonic language. I recently added modification operators that behave rather like a nestable setitem/setattr. Basically:
list_of_objects = list_of_objects <- [index].attr = 'abc'
I've been thinking about ways to make this extensible and flexible (e.g. returning a list of effects to apply to an object, or having some post-processing for any bookkeeping an object needs). The copatterns are pretty cool looking, but I'm not sure how I'd fit it into Mutagen's imperative-esque syntax.
- sqrt17 12y agoIt's simply more general. And it's really easy to write macros that do interesting things. So, incf, which is CL's +=, could be defined as (defmacro (incf lexpr val) `(setf ,lexpr (+ ,lexpr val)) which would evaluate "lexpr" both as a place to put the new value in and as an expression to get the old value out. If you wanted to do that in terms of purely-functional monad application, it gets a bit hairier... Basically, you've got things like "+=" which are mutators (i.e. functions Int->Int). And you've got places, which either (i) take an X mutator and turn it into a mutator for Y, or (ii) take a Y and return an X (accessor). In your example, list_of_objects[index].attr = 'abc' would mean that you take a "constant value" mutator that takes some string and returns "abc" and turn it first into a mutator that changes the .attr value of something ... to something that changes the .attr value of index [index] of the list_of_objects member of the current data. Why would that kind of functional style be useful? Basically, because you could write something like a Python program that still allows for search/nondeterminism/backtracking etc. Some of the weirdness I see in Mutagen seems to come from a partial (or just differing) understanding of how Python and Haskell, respectively, do things. Shoot me a PM if you want to discuss more.
- zwegner 12y agoHaha, I was about to PM you and then realized that there aren't PMs on HN :) I always like to talk about this stuff though, if you want you can email me at zwegner@gmail.com. What you describe is somewhat similar to what I had in mind, but a more well-defined abstraction. I hadn't yet formulated it in terms of types, because for now it's just a hack in the bootstrap interpreter, and anyways a lot of Mutagen's type system is still in my head, waiting to be implemented once I have a better understanding of how/why other functional languages do this stuff.