From: Thomas 'PointedEars' Lahn on
Stefan Weiss wrote:

> VK wrote:
>> if (typeof Moves['01001'] == 'undefined') {
>> Moves['01001'] = 1;
>> }
> ...
>
> I don't see anything intrinsically wrong with that. If strings work for
> you, why not use them. One problem I see with this is when one of the
> dimensions gets larger than 9.

Probably you mean _indexes_ instead of dimensions. This can be worked
around with a delimiter, though:

if (typeof moves['0-1-0-0-1'] == 'undefined')
{
moves['0-1-0-0-1'] = 1;
}

> If you want to go with the nested array approach, you'd need loops to
> create/initialize the structure, or a generator function. Here's a
> simplistic attempt for a dim "replacement", which I'm sure can be
> improved on. For example, the DEFAULT_VALUE could be passed as a third
> argument for each [from, to] group.
>
> var DEFAULT_VALUE = 0;
>
> function dim () {
> if (arguments.length) {
> var args = Array.prototype.splice.call(arguments, 0),
> fromTo = args.shift(),
> start = fromTo[0],
> end = fromTo[1],
> result = [];
> while (start <= end) {
> result[start] = dim.apply(null, args);
> ++start;
> }
> return result;
> }
> return DEFAULT_VALUE;
> }
>
> // Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1)
> var moves = dim([0, 1], [0, 2], [0, 1], [0, 2], [0, 1]);
>
> // ++Moves['01001']
> ++moves[0][1][0][0][1];
>
> console.log(moves);

It is unnecessary to initialize each and every "element" of the "multi-
dimensional array", though. In fact, it wastes a lot of memory. Consider
this instead:

function Matrix()
{
this.data = Array.prototype.slice.call(arguments, 0);
}

Matrix.prototype.putValue = function (x, y, value) {
var tmp = this.data;

for (var i = 0, len = arguments.length; i < len - 2; ++i)
{
var arg = arguments[i];

if (typeof tmp[arg] == "undefined")
{
tmp[arg] = [];
}

tmp = tmp[arg];
}

var lastArgs = Array.prototype.slice.call(arguments, i);
tmp[lastArgs[0]] = lastArgs[1];

return lastArgs[1];
};

Matrix.prototype.getValue = function (x, y) {
var tmp = this.data;

for (var i = 0, len = arguments.length; i < len; ++i)
{
var arg = arguments[i];
tmp = tmp[arg];

if (typeof tmp == "undefined")
{
break;
}
}

return tmp;
};

Matrix.prototype.inc = function (x, y) {
var
coords = Array.prototype.slice(arguments, 0).
v = +this.getValue.apply(this, coords);

return this.putValue.apply(
this, coords.concat((isNaN(v) ? 0 : +v) + 1));
};

var a = new Matrix();
a.inc(0, 1, 0, 0, 1);


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Sean Kinsey on
On Apr 26, 4:09 am, Stefan Weiss <krewech...(a)gmail.com> wrote:
<snip>
> I swear, one of these days I'm going to learn Russian. There's a huge
> part of the web that I don't have access to, and that bugs me.

Haven't you tried Google Chrome with its translation feature?
From: Thomas 'PointedEars' Lahn on
Thomas 'PointedEars' Lahn wrote:

> Matrix.prototype.inc = function (x, y) {
> var
> coords = Array.prototype.slice(arguments, 0).

coords = Array.prototype.slice.call(arguments, 0),

> v = +this.getValue.apply(this, coords);
> [...]

--
PointedEars
From: VK on
On Apr 26, 4:16 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> >   Matrix.prototype.inc = function (x, y) {
> >     var
> >       coords = Array.prototype.slice(arguments, 0).
>
>         coords = Array.prototype.slice.call(arguments, 0),
>
> >       v = +this.getValue.apply(this, coords);

Thank you! Working on the interface right now.
From: Stefan Weiss on
On 26/04/10 13:18, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> var DEFAULT_VALUE = 0;
>>
>> function dim () {
>> if (arguments.length) {
>> var args = Array.prototype.splice.call(arguments, 0),
>> fromTo = args.shift(),
>> start = fromTo[0],
>> end = fromTo[1],
>> result = [];
>> while (start <= end) {
>> result[start] = dim.apply(null, args);
>> ++start;
>> }
>> return result;
>> }
>> return DEFAULT_VALUE;
>> }
>>
>> // Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1)
>> var moves = dim([0, 1], [0, 2], [0, 1], [0, 2], [0, 1]);
>>
>> // ++Moves['01001']
>> ++moves[0][1][0][0][1];
>>
>> console.log(moves);
>
> It is unnecessary to initialize each and every "element" of the "multi-
> dimensional array", though. In fact, it wastes a lot of memory.

In this case, the structure is small enough that it shouldn't matter -
there are only 72 values to initialize (plus the array containers).

> Consider this instead:
>
> function Matrix()
> {
> this.data = Array.prototype.slice.call(arguments, 0);
> }
...

Interesting approach. This is more flexible, and it will only create
values when needed. It also allows you to make the structure assymetric.
For example:

var a = new Matrix();
a.inc(1, 1, 0);
a.inc(0, 1, 0, 0, 1, 1);

That's not equivalent to what Dim does, but since the limits ("0 To 1",
"0 To 2", etc) can't be enforced in JS anyway, your Matrix feels like a
more "natural" fit for JS.


--
stefan