Artifact [13ebf1f544]

Not logged in

Artifact 13ebf1f54403518f13c92e3eb287a31cfbb248a4:

Wiki page [Fin] by andy 2014-09-27 16:00:51.
D 2014-09-27T23:00:51.189
L Fin
N text/x-markdown
U andy
W 1396
#Fin -- Shorthand functions

The next version of JS is slated to support a shorter syntax for functions that simply return the value of an expression. That's great, but something usable now would be nice. Fin supplies a single function `f` which can be used to build shorthand functions:

    f('$ + $')   // === function(a,b) { return a + b; }
    f('$1 + $2') // Same as the previous

Haskell-style "sectioning" is also supported:

    f('+1')      // === function(a) { return a + 1; }
    f('1+')      // Same
    f('+')       // Same as the two functions above
    f('.length') // Object attributes are fine
    f('.run()')  // as are method calls

Sectioning is only done if an infix operator is found *immediately* at the beginning/end of the string. Thus, you can inhibit sectioning by preceding/following the string with some whitespace:

    f(' -1')     // Constant function, always returns -1

Within this shorthand string, single `$` are translated into *successive* numbered `$`s. E.g.,

    `$ + $ * $` 

is translated into

    `$1 + $2 + $3`

Note that this numbering is done ignoring any already-present numbered `$`s.

`$*` is translated into a proper array containing the full list of arguments. 

`$#` is translated into an array containing the "rest" of the argument list, after any numbered `$` arguments (including implicitly numbered `$`s). 
Z 33211cfc29401e911b8ded04c8429c4a