Permalink
Browse files

extend row(), col() to support callbacks. match docs and tests.

  • Loading branch information...
1 parent d649246 commit 39ee718dd6babfb5aeace06019b2b98e8d977d7d @trevnorris trevnorris committed Jun 4, 2012
Showing with 69 additions and 15 deletions.
  1. +32 −4 doc/md/core.md
  2. +19 −11 src/core.js
  3. +9 −0 test/core/col-test.js
  4. +9 −0 test/core/row-test.js
View
@@ -73,6 +73,7 @@ Returns the count of rows in a matrix.
Or pass a callback to run the calculation asynchronously and pass on the calculation.
This allows for continued chaining of methods to the jStat object.
+Also note `this` within the callback refers to the calling jStat object.
jStat( matrix ).rows(function( d ) {
// d === 2
@@ -93,6 +94,7 @@ Returns the number of columns in a matrix.
Or pass a callback to run the calculation asynchronously and pass on the calculation.
This allows for continued chaining of methods to the jStat object.
+Also note `this` within the callback refers to the calling jStat object.
jStat( matrix ).cols(function( d ) {
// d === 3
@@ -113,28 +115,54 @@ Returns and object with the dimensions of a matrix.
Or pass a callback to run the calculation asynchronously and pass on the calculation.
This allows for continued chaining of methods to the jStat object.
+Also note `this` within the callback refers to the calling jStat object.
jStat( matrix ).dimensions(function( d ) {
// d === { cols: 3, rows: 2 }
});
### row()
-Returns a specified row of an array or jStat object as a vector.
+Returns a specified row of a matrix.
+
+**row( array, index )**
var matrix = [[1,2,3],[4,5,6]];
- jStat( matrix ).row( 0 ) === jStat([1,2,3]);
jStat.row( matrix, 0 ) === [1,2,3];
+**fn.row( index[, callback] )**
+
+ jStat( matrix ).row( 0 ) === jStat([1,2,3]);
+
+Or pass a callback to run the calculation asynchronously and pass on the calculation.
+This allows for continued chaining of methods to the jStat object.
+Also note `this` within the callback refers to the calling jStat object.
+
+ jStat( matrix ).row( 0, function( d ) {
+ // d === jStat([1,2,3])
+ });
-### col( index )
+### col()
Returns the specified column as a column vector.
+**col( index )**
+
var matrix = [[1,2],[3,4]];
- jStat( matrix ).col( 0 ) === jStat([[1],[3]]);
jStat.col( matrix, 0 ) === [[1],[3]];
+**fn.col( index[, callback] )**
+
+ jStat( matrix ).col( 0 ) === jStat([[1],[3]]);
+
+Or pass a callback to run the calculation asynchronously and pass on the calculation.
+This allows for continued chaining of methods to the jStat object.
+Also note `this` within the callback refers to the calling jStat object.
+
+ jStat( matrix ).col( 0, function( d ) {
+ // d === jStat([[1],[3]])
+ })
+
### diag()
Returns the diagonal of the matrix.
View
@@ -299,7 +299,7 @@ jStat.extend({
results;
// check for callback
if ( func ) {
- setTimeout( function() {
+ setTimeout(function() {
func.call( tmpthis, jStat.fn[ passfunc ].call( tmpthis ));
}, 15 );
return this;
@@ -310,6 +310,23 @@ jStat.extend({
})( funcs[i] );
})( 'transpose clear symmetric rows cols dimensions diag antidiag'.split( ' ' ));
+// extend jStat.fn with methods that have one argument
+(function( funcs ) {
+ for ( var i = 0; i < funcs.length; i++ ) (function( passfunc ) {
+ jStat.fn[ passfunc ] = function( index, func ) {
+ var tmpthis = this;
+ // check for callback
+ if ( func ) {
+ setTimeout(function() {
+ func.call( tmpthis, jStat.fn[ passfunc ].call( tmpthis, index ));
+ }, 15 );
+ return this;
+ }
+ return jStat( jStat[ passfunc ]( this, index ));
+ };
+ })( funcs[i] );
+})( 'row col'.split( ' ' ));
+
// extend jStat.fn with simple shortcut methods
(function( funcs ) {
for ( var i = 0; i < funcs.length; i++ ) (function( passfunc ) {
@@ -320,18 +337,9 @@ jStat.extend({
})( 'create zeros ones rand identity'.split( ' ' ));
// extend jStat.fn
+// specialized instance methods that can't have generalized assignments
jStat.extend( jStat.fn, {
- // Returns a specified row as a vector
- row : function( index ) {
- return jStat( jStat.row( this, index ));
- },
-
- // Returns the specified column as a vector
- col : function( index ) {
- return jStat( jStat.col( this, index ));
- },
-
// map a function to a matrix or vector
map : function( func, toAlter ) {
return jStat( jStat.map( this, func, toAlter ));
View
@@ -13,6 +13,15 @@ suite.addBatch({
assert.deepEqual( jStat.col([[1,2],[3,4]], 1 ), [[2],[4]] );
assert.deepEqual( jStat([[1,2],[3,4]]).col( 1 ).toArray(), [[2],[4]] );
}
+ },
+ 'fn.col' : {
+ 'topic' : function() {
+ jStat([[1,2],[3,4]]).col( 0, this.callback );
+ },
+ 'col callback' : function( val, stat ) {
+ assert.deepEqual( val.toArray(), [[1],[3]] );
+ assert.isTrue( this instanceof jStat );
+ }
}
});
View
@@ -13,6 +13,15 @@ suite.addBatch({
assert.deepEqual( jStat.row([[1,2],[3,4]], 1 ), [3,4] );
assert.deepEqual( jStat([[1,2],[3,4]]).row( 1 ).toArray(), [3,4] );
}
+ },
+ 'fn.row' : {
+ 'topic' : function() {
+ jStat([[1,2],[3,4]]).row( 0, this.callback );
+ },
+ 'row callback' : function( val, stat ) {
+ assert.deepEqual( val.toArray(), [1,2] );
+ assert.isTrue( this instanceof jStat );
+ }
}
});

0 comments on commit 39ee718

Please sign in to comment.