Created
June 9, 2011 17:22
-
-
Save watagashi/1017218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.method = function (name, func) { | |
if (!this.prototype[name]) { | |
this.prototype[name] = func; | |
return this; | |
} | |
}; | |
Array.method('reduce', function (f, value) { | |
var i; | |
for (i = 0; i < this.length; i+= 1) { | |
value = f(this[i].value); | |
} | |
}); | |
var data = [4, 8, 15, 16, 23, 42]; | |
var add = function (a, b) { | |
return a + b; | |
}; | |
var mult = function (a, b) { | |
return a * b; | |
}; | |
var sum = data.reduce(add, 0); | |
data.total = function () { | |
return this.reduce(add, 0); | |
}; | |
// 6.7 | |
Array.dim = function (dimention, initial) { | |
var a = [], i; | |
for ( i = 0; i < dimention; i += 1) { | |
a[i] = initial; | |
} | |
return a; | |
}; | |
var myArray = Array.dim(10, 0); | |
var matrix = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8] | |
]; | |
for (i = 0; i < n; i += 1) { | |
my_array[i] = []; | |
} | |
Array.matrix = function (m, n, initial) { | |
var a, i, j, mat = []; | |
for (i = 0; i < m; i += 1) { | |
a = []; | |
for (j = 0; j < n; j += 1) { | |
a[j] = initial; | |
} | |
mat[i] = a; | |
} | |
return mat; | |
} | |
var myMatrix = Array.matrix(4, 4, 0); | |
Array.identity = function (n) { | |
var i, mat = Array.matrix(n, n, 0); | |
for (i = 0; i < n; i += 1) { | |
mat[i][i] = 1; | |
} | |
return mat; | |
}; | |
myMatrix = Array.identity(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment