4

I am still new to JavaScript. I need to code a 20 x 20 matrix pair of functions. All my functions take in a number in and return a number (i.e, same signature). For example, Myfunctions[1][2] should return me a pair of functions I could call in the code.

In Java, I would typically implement an array of 20 x 20 objects where each object would implement two functions. But, is this possible in JavaScript? If not, how should I proceed to get something similar? If I need two matrix to simulate the pair, this is OK too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453

6 Answers6

8

Since functions are objects in JavaScript, you can define an array of them quite easily.

function foo () { ... }
function bar () { ... }
function baz () { ... }

var fns = [foo, bar, baz];

The signature does not matter at all.


From there you can start dynamically generating functions in a loop, rather than explicitly declaring each one:

function generator(n)
{
    return function ()
    {
        return n*n;
    };
}

var squareFuncs = [];

for (var i=0; i<10; i++)
{
    squareFuncs.push(generator(i));
}

Then you can build up arrays of arrays of functions (just like any other object, remember):

function anotherGenerator(a, b)
{
    return function ()
    {
        return a+b;
    };
}

var sumFuncs = [],
    temp,
    i,
    j;

for (i=0; i<20; i++)
{
    temp = [];
    for (j=0; j<20; j++)
    {
        temp.push(anotherGenerator(i, j));
    }
    sumFuncs.push(temp);
}

Now sumFuncs is a two-dimensional array (really, an array of arrays) of functions which compute the sum of the coordinates of that function in the matrix. That probably sounds more complicated than it really is, so here's an example:

var foo = sumFuncs[7][2],
    sum = foo();
console.log(sum); // prints 9

Related:

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
3

You cannot really have a matrix array structure in ECMAscript, but you can create Arrays of Arrays of course:

function o1() {};
function o2() {};
function o3() {};
function o4() {};
function o5() {};
function o6() {};

var Myfunctions = [
    [ o1, o2 ],
    [ o3, o4 ],
    [ o5, o6 ]
];

Now

Myfunctions[0][1]();

would execute function o2 in the above example.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 1
    I don't mean any offence to jAndy, but why is this the accepted answer? Yes, it explains very nicely how to have a two-dimensional array of function references (or the JS equivalent, an array of arrays), but the question explicitly states "Myfunctions[1][2] should return me **a pair of functions** I could call in the code". I have no idea how "a pair of functions" would be used, but it is mentioned several times in the question... – nnnnnn Nov 28 '11 at 03:26
  • That's not the only information in the question. There is also "If I need two matrix to simulate the pair, this is ok too." – Jérôme Verstrynge Nov 28 '11 at 03:35
  • 1
    OK, well, fair enough. (Except that you _don't_ need to use two matrixes to simulate the pair. You can easily get a `MyFunctions[1][2].a()` and `MyFunctions[1][2].b()` syntax as shown in a couple of other answers.) – nnnnnn Nov 28 '11 at 03:55
2

You can create an object with two functions and put that into your array.

BNL
  • 7,085
  • 4
  • 27
  • 32
1

Consider using a function factory instead:

function makeFunction(one, two) {
  return function () {
    ...
  }
}

makeFunction(1,2);
// Not functionMatrix[1][2]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • I guess this could be useful if `makeFunction` has a closure that effects the results. Makes my head spin to think about. – Hogan Nov 28 '11 at 02:47
1
Myfunctions[1][2] = { 
    first: function(val) { return val + 1; }
    second: function(val) { return val - 1; }
};

firstVal = Myfunctions[1][2].first(100);
secondVal = Myfunctions[1][2].second(100);
AaronHolland
  • 1,595
  • 1
  • 16
  • 32
0

Here is a two-dimensional array of pairs of functions:

Almost all of them are anonymous functions except for one to show you how that would work.

You can use this like this: var myresult = myFuncs[2][3].func1(45);

function extern2ArrayFunc (a) {
  return a+;
}

var myFuncs = [
    [ { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : exter2ArrayFunc },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      }
    ],
    [ { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      }
    ],
    [ { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      },
      { "func1" : function (a) { return a+1; },
        "func2" : function (b) { return b-1; }
      }
    ]
  ];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • My eyes, they bleed. Can we please have a single function and 20 references. – Raynos Nov 28 '11 at 02:30
  • @Raynos - That is clearly **EXACTLY NOT** what the OP asked for. – Hogan Nov 28 '11 at 02:39
  • @Raynos - A single function with 20 references to that same function kind of makes the whole array structure pretty pointless, but if indeed the real problem requires different functions this answer shows how to do it. – nnnnnn Nov 28 '11 at 02:42
  • Some of my functions will be re-used in several cells, so I don't want to have to rewrite them. – Jérôme Verstrynge Nov 28 '11 at 02:50
  • @JVerstry - Then use the extern to the array method I showed there. I put it in one place but you can put it in as many as you want. – Hogan Nov 28 '11 at 02:54