Comments on: FOREACH on objects
Here's a common coding pattern: iterating over the words (and values) of an object:
foreach word words-of my-object [
The suggestion was made to support such a loop directly:
foreach word my-object [
and also allow:
foreach [word value] my-object [
do-something-else-with word value
The benefit is that no intermediate block of words is constructed.
This change has been added to the A49 release.
>> foreach word system/options [?? word]
Note that the word is bound back to the object, so can be used to access the value (with get).
However, it also supports:
>> foreach [word value] system/options [print [word mold value]]
file-types [%.bmp bmp %.gif gif %.png png %.jpg %.jpeg jpeg]
Be careful with hidden (protected) variables. The loop will skip them:
>> foreach word system/state [?? word]
The state object contains more than two fields, but they are not accessible to user code for security reasons.
This has been one of my biggest requests for a while now, as there is a lot of mezzanine code that could be sped up using this method. A few questions:
- Does this work for map! too?
- Is the FOREACH [obj: word value] variant also supported? The obj: would be set to a reference to the object.
- What happens if a field is appended to the object while the loop is running?
BrianH - where does the speed-up come from? Is it because we are not creating a copy of object words? Can we also say, that we save memory here?|
Yup, that's where part of the speedup comes from.
The rest of the speedup comes from not having to check types and behave differently depending on whether you are working on a block or an object. Every time we add some feature that lets us treat different types in similar ways, it cuts down on special case code.
An example is SELECT, which now works on blocks, maps and objects in pretty much the same way, making these types interchangeable in your code without EITHER or CASE needed. Reducing the need for these cuts down of comparison overhead and code duplication.
Brian, yes, and actually, if you hadn't posted it to CureCode, I'm sure it would not have happened.
On above questions:
1. yes, same as objects, except skips "empty slots" (none! values). Also should work on modules, ports, and errors (but needs to be tested by the comm.)
2. It's an error (or should be)
3. Includes appended values. (But, we can change that if we want. Which do we prefer?)
Pekr: ditto what Brian stated. Both mem and speed. The main thing is the common pattern - nice to have supported.
Followups (no links because of comment screening):
1. Ticket created, #722.
2. It's a request (#723), though not really that necessary :)
3. I prefer to include appended values because that would be less surprising to REBOL coders. This is the same as option 4 in ticket #722.
BTW, if you are going to be screening comment posts for links and accusing the person of duplicate posting, wouldn't it be better to just not allow a tags in the first place?
Post a Comment:
You can post a comment here. Keep it on-topic.