REBOL3 Syntax

People sometimes ask if the syntax in R3 is different from R2. Is R3 code hard or too different to read, when you are a hardened R2 user? Is R3 harder to start with for a beginner?

The answer is no. If you use R2, you will be able to use R3 immediately! If you are a beginner, the learning curve is roughly the same. Perhaps less, because of the better documentation.

The main changes happen on a function/word level. You also probably need to learn a few new dialects, like VID3. Some functions are rewritten to behave differently, while other new functions help shortening the amount of code you need to write. In fact R3 code is often simpler to read, because there is simply less of it.

A simple example is the new TAKE function:

>> a: [1 2 3 4 5]
>> take/part a 2
== [1 2]
>> a
== [3 4 5]

To do the same thing in R2 requires a bit more code and an additional word that stores the result we want:

>> a: [1 2 3 4 5]
>> b: copy/part a 2
== [1 2]
>> remove/part a 2
>> [3 4 5]

It's easy to find many examples of this and it's done by observing general patterns of code in R2 that can be simplified. Some are simple like in the example above that reduces 2 function calls to 1, while others are more complex. Particularly operations on series have improved, such as:

Moving elements around in a block with a MOVE function. It uses the same indexing as INSERT, hence the extra HEAD in the example. This function is not officially in R3 yet, still in testing.

>> a: [first second third fourth fifth]
== [first second third fourth fifth]
>> head move a 4
== [second third fourth fifth first]

Generating a new block from the content of an existing block with the MAP function. This can be used to flatten blocks very quickly or to collect columns of data and format it for a new block.

>> a: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
>> map [b c] a [b + c]
== [3 7 11]

Alter a block during a loop with FOREACH and MAP. FORALL is no longer the only viable way to do that.

Say you have a flat block with 3 recurring elements:

[a b c]

Knowing FOREACH from R2, you can assign a word to each of those 3 elements and get the element you need from the block quickly inside your loop.

R3 adds set-word! possibilities:

[a b d: c]

This means that the index where C is at in the block is placed at D, which allows you to change the block directly using D using the known block manipulation functions.

>> a: [1 2 3 4 5 6 7 8 9]
== [1 2 3 4 5 6 7 8 9]
>> foreach [a b d: c] [change d 'hello]
== [1 2 hello 4 5 hello 7 8 hello]

Very nice and elegant way to do that!
|