GithubHelp home page GithubHelp logo

glathoud / transfun Goto Github PK

View Code? Open in Web Editor NEW
44.0 3.0 2.0 2.23 MB

Merging loops for speed in JavaScript

Home Page: http://glat.info/transfun

License: Boost Software License 1.0

JavaScript 63.68% HTML 35.62% Shell 0.03% D 0.67%
loop javascript merging-loops language optimization performance code-generation

transfun's People

Contributors

glathoud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

transfun's Issues

tpub: Remove the arity-0 shortcut because of a design issue (chaining)

Current state

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)

Design issue: chaining

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()...

Task: favor (relative) implementation simplicity and drop the shortcut

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.

add support for ranges

...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;
}

investigate wrong external bind

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}')
;

speedup transfun itself: try to cache the generated code

probably along thes lines:

  • an id for each transfun instance
  • update/read cache based of (transfun_id, transfun_arguments), for string arguments a trie structure, for function arguments a (Weak)Map or array fallback for it.

npm test broken because of recent changes

(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)

Buildtime compile

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.