| Applying science to business management |
  
Lists are Copied and Compared by Value
In JavaScript, lists are copied and compared by reference. In
DScript, lists are a fundamental data type and are copied and
compared by value, just like numbers and text strings.
Consider the following code example:
var x=[1,2,3,4,5];
var y=x;
y[0]=99;
In JavaScript, the statement y=x
does not copy the entire contents of the list x into y.
Instead, the value of y is just a pointer to x.
This means that after the statement y[0]=99
is executed, the value of x is [99,2,3,4,5], even though x
is never explicitly modified. Compare this result to that of code
using scalars:
var x=1;
var y=x;
y=99;
After this code executes, x will have a value of 1, and
y will be 99.
In JavaScript, scalars are copied by value and lists are
copied by reference. This is done to make list copying more
efficient. However, DScript achieves the same efficiency while
still copying all data (including lists) by value.
Consider the original example again. This time the example is
run in DScript.
var x=[1,2,3,4,5];
var y=x;
y[0]=99;
The first statement, x=[1,2,3,4,5]
behaves in exactly the same manner in both DScript and
JavaScript. In JavaScript, the second statement, y=x, sets y equal to a pointer
to x. This is also what DScript does. Now,
consider what happens in the last statement, y[0]=99. JavaScript will modify the
list to which both x and y point. DScript, however,
will realize that x and y point to the same list;
so, a copy of the list is made before the modification takes
place. When the code is finished, the value of both x and y
in JavaScript is [99,2,3,4,5]. In DScript, x is
[1,2,3,4,5] and y is [99,2,3,4,5].
DScript keeps the efficiency of copying lists with pointers
while using the pointers intelligently enough that the result is
just as if the list was copied by value.
The by-reference versus by-value difference in the way DScript
and JavaScript treat lists is also evident in comparisons. For
example, the comparison statement x==y
below will evaluate to false in JavaScript, but it will be
true in DScript.
var x=[1,2,3,4,5];
var y=[1,2,3,4,5];
if(x==y)
...;
The differences here might seem subtle, but they are extremely
important. In DScript, a list is a simple data type, like a
number or a text string. Lists are copied and compared by value,
just like all other data types. This feature, coupled with list
and matrix processing, adds greatly to DScript's abilities. For
example, if you want to add all elements in matrix B to
the elements in matrix A, you would use JavaScript code
like this:
for(var x=0; A[x][0]!=null; x++) {
for(var y=0; A[x][y]!=null; y++)
A[x][y]+=B[x][y];
}
Using DScript you can do the same thing with a simple
statement like this:
A+=B;
Also, the DScript code will execute at about 30 times the
speed of the equivalent JavaScript code.
In DScript, if you want to pass a list pointer to a function
so that the function can modify the original list, just define
the function arguments with the @ character as in the following
example:
Increment(@list):={
list+=1;
}
var x=[1,2,3,4,5];
Increment(x);
|