Skip to content

Commit

Permalink
#12 self-inlining (of appfun externals)
Browse files Browse the repository at this point in the history
  • Loading branch information
glathoud committed Aug 8, 2018
1 parent 6104979 commit 9674d95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,36 @@ var global, exports;
, obtained = quite_protracted_and_synthetic_example_3( 7, 8 )
;
expected === obtained || null.bug;

// Inlining of generated appfun when used as external
// functions, see:
// https://github.com/glathoud/transfun/issues/12

var get_mean = tfun.redinit( '0', '+' ).next( '/n' )
, get_stddev = tfun
.decl( 'mean_value', get_mean )
.map( 'v-mean_value' )
.map( 'v*v' )
.sum()
.next( 'Math.sqrt(current/(n-1))' ) // unbiased std dev estimation

// compute both mean and stddev at once
, get_mean_stddev = get_stddev
.next( '{stddev:current, mean:mean_value}')
;

var arr = [ 0.1, 0.2, 0.345, 0.456, 0.42342, 0.12 ]
, arr_mean = arr.reduce( function ( a, b ) { return a+b; }, 0 ) / arr.length
, arr_std = Math.sqrt( arr.reduce( function ( a, b ) { return a+(b-arr_mean)*(b-arr_mean); }, 0 ) / (arr.length - 1) )

, o = get_mean_stddev( arr )
;
1e-10 > Math.abs( o.mean - arr_mean ) || null.bug;
1e-10 > Math.abs( o.stddev - arr_std ) || null.bug;

!/extern/.test( get_mean_stddev.getBodyCode() ) || null.bug;
// xxx !/\bvar\b/.test( get_mean_stddev.getBodyCode() ) || null.bug;

//

console.timeEnd( 'transfun:test' );
Expand Down
5 changes: 5 additions & 0 deletions transfun.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,11 @@ var global, exports; // NPM support [github#1]
appfun.getNExternal = appfun_getNExternal;

cached_appfun = chainspec.appfun = mix_published_tfun_methods_into_appfun( chainspec, appfun );

// API for inlining when appfun are used as externals
// https://github.com/glathoud/transfun/issues/12
appfun[ _TF_GET_BODYCODE ] = appfun_getBodyCode;
appfun[ _TF_GET_ARGNAME_ARR ] = function () { return code_par_arr.slice( extern_arr.length ); };
}
return cached_appfun;

Expand Down

0 comments on commit 9674d95

Please sign in to comment.