glathoud / transfun Goto Github PK
View Code? Open in Web Editor NEWMerging loops for speed in JavaScript
Home Page: http://glat.info/transfun
License: Boost Software License 1.0
Merging loops for speed in JavaScript
Home Page: http://glat.info/transfun
License: Boost Software License 1.0
try it!
While published arity>0 transfun require 2 calls (standard case):
var transfun = tfun.map;
var appfun = transfun( '.p' );
...the arity-0 transfuns are immediately published:
var appfun = tfun.and;
This was meant to "simplify" life, by preventing "ugly" stuff like: tfun.and()(someArray)
When publishing an arity-0 transfun, because of the shortcut, we publish it directly as an appfun (=ready-to-use function). So far, fine. But we have to propagate it as a property of other published transfun/appfuns as well (including the published appfun itself!), which defines an infinite structure.
To take a contrived but hopefully clear example:
tfun.and.and.and....
We can't implement an infinite structure, unless using special getters, and I don't want to go there.
Side note: the current implementation (2016-07-07) isn't even consistent with the design:
tfun.and.and().and()...
Side note: it might also be simpler for people to understand, when all transfuns are used the same way, whether they have arity 0 or not.
Side note: update the article as well.
...for example to generate an array of numbers:
tfun.range()( 3, 7 )
// >>> [3, 4, 5, 6]
...or start a loop based on a range, then modify it with map, filter etc., and generate code that has a single loop:
// pick a subset [begin...end[ of a sparse subarray `arr` (`arr` could also be an object)
tfun.arg( 'arr,begin,end' ).rangeOf( 'begin', 'end' ).filter( 'v in arr' ).map( 'arr[ v ]' )
// >>> function anonymous(current
/**/) {
var arr = arguments[ 0 ];
var begin = arguments[ 1 ];
var end = arguments[ 2 ];
var out = [];
for (var v = begin; v < end; v++) {
/* -- rangeOf( `begin`, `end` ) -- */
/* -- filter( `v in arr` ) -- */
if ((v in arr)) {
/* -- map( `arr[ v ]` ) -- */
out.push((arr[ v ]));
}
}
return out;
}
According to https://stackoverflow.com/questions/1546981/post-increment-vs-pre-increment-javascript-optimization +=1 is even faster than ++i, which is faster than i++.
Reason: not so interesting for a simple use case, where ldc2 -O
is already doing a great job to optimize map/filter etc.: https://github.com/glathoud/transfun/tree/master/misc/d_performance_feasability
but there could be more interesting, slightly more complex use cases where transfun is interesting. At least interesting to try and know the answer.
Below, why is get_mean_stddev bound
to an external function using transfun's current implementation?
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}')
;
probably along thes lines:
Can we speed up transfun and/or integrate with functions defined using Fast Explicit Tail Calls
(probably because of #7 )
npm test
...
0 passing (87ms)
1 failing
1) all
should return true, and not throw any error:
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at Function (native)
at ensure_appimpl (transfun.js:987:37)
at Function.transfun [as map] (transfun.js:891:21)
at test (test.js:556:61)
Is there (an easy way to implement|plans to include|another project) that does this transform from functional methods to one loop as a build step?
web workers, for appfuns and for normal functions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.