Modified assignment with objects: should it work?
6 posts
• Page 1 of 1
Modified assignment with objects: should it work?
In Dyalog APL, one can use modified assignment, e.g. +← with ordinary variables, including indexed arrays. The question is: Should this work in a comparable way with class instances (class-based objects)?. Specifically, should it work as expected with methods defined via keywords :Property Default Keyed MyMethod in examples like
1. APL allows it; it returns no error; it's happily wrong.
2. It seems to work properly on the first key in the vector 1 2 3, but not apply the modifying function + on subsequent elements.
3. I can't find documentation or examples that might guide me. (And yes, I can't count points properly).
Any experience out there?
- Code: Select all
a[1 2 3]+←10 100 1000 ⍝ where ¨a¨ is an object defined via ⎕NEW
1. APL allows it; it returns no error; it's happily wrong.
2. It seems to work properly on the first key in the vector 1 2 3, but not apply the modifying function + on subsequent elements.
3. I can't find documentation or examples that might guide me. (And yes, I can't count points properly).
Any experience out there?
- petermsiegel
- Posts: 160
- Joined: Thu Nov 11, 2010 11:04 pm
Re: Modified assignment with objects: should it work?
Hi Peter!
Yes, it should work, with caveats like there being no guarantees on how many times the get and set functions will be called. However, I see that it does NOT work properly, in the simple example that I constructed, only the first index was updated.
I will log an issue and we'll get back to you when it is fixed.
Having said that, I personally avoid the "f←" construct. It only does what you think when f is a scalar primitive; if f is a defined function there is an implied each and performance goes to the dogs. If I were to start again with a blank sheet, my new APL interpreter would not implement this shortcut. It only had one real use case which was efficient frequency counting using repeated indices, but now we have Key for that.
Yes, it should work, with caveats like there being no guarantees on how many times the get and set functions will be called. However, I see that it does NOT work properly, in the simple example that I constructed, only the first index was updated.
I will log an issue and we'll get back to you when it is fixed.
Having said that, I personally avoid the "f←" construct. It only does what you think when f is a scalar primitive; if f is a defined function there is an implied each and performance goes to the dogs. If I were to start again with a blank sheet, my new APL interpreter would not implement this shortcut. It only had one real use case which was efficient frequency counting using repeated indices, but now we have Key for that.
-
Morten|Dyalog - Posts: 462
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Modified assignment with objects: should it work?
Morten,
Thanks for testing and updating me/us. I agree it might/should be avoided-- in fact, I had already written a separate method myDict.AddTo to update frequencies of all keys found, but wanted to check the idiomatic myDict+← case for completeness (and to see if there was a performance advantage for either case).
More a curiosity and from my habit of thinking pedagogically about what students will run into.
Best
Thanks for testing and updating me/us. I agree it might/should be avoided-- in fact, I had already written a separate method myDict.AddTo to update frequencies of all keys found, but wanted to check the idiomatic myDict+← case for completeness (and to see if there was a performance advantage for either case).
More a curiosity and from my habit of thinking pedagogically about what students will run into.
Best
- petermsiegel
- Posts: 160
- Joined: Thu Nov 11, 2010 11:04 pm
Re: Modified assignment with objects: should it work?
Well we've fixed the limitation in 18.2 and 19.0, and will be available in the next DSS update. We will also be issuing updated 18.2 installation images for all platforms for all users in the near future. New 19.0 images for all platforms for all users will be made available in a slightly longer timeframe.
-
AndyS|Dyalog - Posts: 263
- Joined: Tue May 12, 2009 6:06 pm
Re: Modified assignment with objects: should it work?
Actually, I should qualify the statement "we've fixed the limitation" .. for the Keyed Property a,
a[1 2 3]+←10 100 1000will work in the forthcoming updates for 18.2 onwards, but for the related
a[2 2]+←10 ⍝ Duplicate indicesand
a[1;1]+←3 ⍝ Higher rankstill both fail - and fixes for these last two will take rather to produce than the first !
-
AndyS|Dyalog - Posts: 263
- Joined: Tue May 12, 2009 6:06 pm
Re: Modified assignment with objects: should it work?
Thanks all for your diligent work. The semantics of repeated indices (with or without keys) can be surprising, especially for novices. Even for "regular" APL objects (and aficionados), we don't think very often about how serial (one at a time, left-to-right) operations like modified assignment +← must be when indexed arrays are the target, to wit:
- Code: Select all
a←0 0 0 ⍝ vanilla vector
a[1 2 1 2 1 1]+← 1
a
0 4 2
- petermsiegel
- Posts: 160
- Joined: Thu Nov 11, 2010 11:04 pm
6 posts
• Page 1 of 1
Return to Object Oriented Programming
Who is online
Users browsing this forum: No registered users and 0 guests
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group