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