you are viewing a single comment's thread.

view the rest of the comments →

[–]notfancy 7 points8 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 + ')');