Comments on: FOREACH on objects

Carl Sassenrath, CTO
REBOL Technologies
23-Apr-2009 17:55 GMT

Article #0192
Main page || Index || Prior Article [0191] || Next Article [0193] || 5 Comments || Send feedback

Here's a common coding pattern: iterating over the words (and values) of an object:

foreach word words-of my-object [
     do-something-with word

The suggestion was made to support such a loop directly:

foreach word my-object [
     do-something-with word

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]
word: boot
word: home
word: path
word: flags
word: script
word: args
word: do-arg
word: import
word: debug
word: secure
word: version
word: quiet
word: binary-base
word: decimal-digits
word: module-paths
word: file-types

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]]
boot %/C/rebol/r3/make/view/Debug/view.exe
home %/C/rebol/r3/make/
path %/C/rebol/r3/make/view/Debug/
flags [true]
script none
args none
do-arg none
import none
debug none
secure none
version none
quiet false
binary-base 16
decimal-digits 15
module-paths [%./]
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]
word: note
word: last-error

The state object contains more than two fields, but they are not accessible to user code for security reasons.



Brian Hawley
23-Apr-2009 15:14:50

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:

  1. Does this work for map! too?
  2. Is the FOREACH [obj: word value] variant also supported? The obj: would be set to a reference to the object.
  3. What happens if a field is appended to the object while the loop is running?
24-Apr-2009 3:14:29
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?
Brian Hawley
24-Apr-2009 13:23:18
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.

Carl Sassenrath
24-Apr-2009 19:05:08
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.

Brian Hawley
24-Apr-2009 20:30:25
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.


Blog id:



 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

Updated 15-Jun-2024 - Edit - Copyright REBOL Technologies -