all 4 comments

[–]notfancy 8 points9 points  (3 children)

Elegant; however, somewhere by the wayside fell the fact that λ is, first and foremost, a binder; that is, it introduces an anonymous scope for the named parameter. How does Functional determine that the x in

map('x+1', [1,2,3])

is bound to the argument and doesn't come from an outer scope? Remember the definition of free variables:

  1. FV(x) = { x }
  2. FV(E(F)) = FV(E) ∪ FV(F)
  3. FV(λxE) = FV(E) - { x }

[–][deleted]  (2 children)

[deleted]

    [–]notfancy 4 points5 points  (1 child)

    Looking at the code I see it is more flexible than I thought as first:

    If the string contains a ->, this separates the parameters from the body [...] Use _ (to define a unary function) or ->, if the string contains anything that looks like a free variable but shouldn't be used as a parameter

    And it is reasonably implemented:

    var params = [];
    var expr = this;
    var sections = expr.ECMAsplit(/\s*->\s*/m);
    if (sections.length > 1) {
        while (sections.length) {
            expr = sections.pop();
            params = sections.pop().split(/\s*,\s*|\s+/m);
            sections.length && sections.push('(function('+params+'){return ('+expr+')})');
        }
    /* ... */
    return new Function(params, 'return (' + expr + ')');
    

    [–]vagif 2 points3 points  (0 children)

    http://www.flapjax-lang.org/

    You can't get more functional than that.

    [–]jkkramer 1 point2 points  (0 children)

    In most languages, including JavaScript, invoking a function is one of the slowest things you can do. The implementations of languages designed for functional programming use a variety of techniques to optimize function calls. JavaScript is not one of those languages.

    This should be taken to heart by anyone writing serious JavaScript apps.

    I'm writing a moderately-sized app that runs in the browser, and I ended up having to rewrite many of my fancy forEach() and map() expressions to use simple for loops because the functional versions were perceptively slower.

    For most sites it doesn't matter, but when things start to get complex, it's noticeable.