Bob Myers (2017-07-15T03:11:47.000Z)
rtm at gol.com (2017-07-15T03:21:04.911Z)
The proposal to write `arr.reduce((+))`, with `(+)` as an alternative to `(a, b) => a + b` is admirably concise, but syntactically challenging. I propose an alternative which is slightly less compact, but hopefully more implementable and general. The idea is a new form of function we'll call a "pound function", written as `#{ }`, Within the body, parameters are available as `#0`, `#1`, etc. Thus, the example about would be written as ```js arr.reduce(#{#0 + #1}) ``` Within the body of the pound function, we adopt the convention that pound signs with no following number are assigned to the arguments in the lexically encountered order, allowing us to write ```js arr.reduce(#{# + #}) arr.sort(#{#.order - #.order}) ``` This syntax gets around the problem of whether `(-)` is unary or binary: ```js const negate = #{-#}; const subtract = #{# - #}; ``` In this proposal, there is no version of pound functions with multiple statements and/or return statements. If your function body is that complex, write a regular function. Pound functions are meant for tiny one-line function snippets. If need be, we can define `...##` inside pound functions as referring to the argument list, so ```js const sumParams = #{##.reduce(#{# + #})}; ``` Ugh. Anyway, I will leave it others to opine on whether this cryptic syntax is worth the trouble, issues related to nested pound functions, etc. etc. Of course, it would be nice if we could arrange to skip the `#{}` altogether and just write `reduce(# + #)`. The problem is that such "naked" pound functions are ambiguous; since `#` by itself would mean `a => a`, we don't know if `# + #` is supposed to mean `(a => a) + (a => a)`, or `(a, b) = a + b`. In theory, we could avoid this issue by introducing the rule that any expression involving one or more `#` or `#n` is considered to be a single pound function, so `# + #` would mean `(a, b) => a + b`, as we presumably want. However, that would prevent us from using nested pound functions; if we really want to write the equivalent of `a => foo(a, b => b)`, we could no longer write `foo(#, #)`, because this would be interpreted as `(a, b) => foo(a, b)`. An extremely awkward solution to this possibly subcase of nested pound functions would be to introduce a `##` syntax, where sub-expressions in which `##` are encountered are treated as separate pound functions, allowing us to write `foo(#, ##)`. Or, allow the explicit pound function syntax in this case, making it `foo(#, #{#})`. But that is probably too cryptic for even the most avid syntax hackers. Bob
rtm at gol.com (2017-07-15T03:19:53.919Z)
The proposal to write `arr.reduce((+))`, with `(+)` as an alternative to `(a, b) => a + b` is admirably concise, but syntactically challenging. I propose an alternative which is slightly less compact, but hopefully more implementable and general. The idea is a new form of function we'll call a "pound function", written as `#{ }`, Within the body, parameters are available as `#0`, `#1`, etc. Thus, the example about would be written as ```js arr.reduce(#{#0 + #1}) ``` Within the body of the pound function, we adopt the convention that pound signs with no following number are assigned to the arguments in the lexically encountered order, allowing us to write ```js arr.reduce(#{# + #}) arr.sort(#{#.order - #.order}) ``` This syntax gets around the problem of whether `(-)` is unary or binary: ```js const negate = #{-#}; const subtract = #{# - #}; ``` In this proposal, there is no version of pound functions with multiple statements and/or return statements. If your function body is that complex, write a regular function. Pound functions are meant for tiny one-line function snippets. If need be, we can define `...##` inside pound functions as referring to the argument list, so ```js const sumParams = #{##.reduce(#{# + #})}; ``` Ugh. Anyway, I will leave it others to opine on whether this cryptic syntax is worth the trouble, issues related to nested pound functions, etc. etc. Of course, it would be nice if we could arrange to skip the `#{}` altogether and just write `reduce(# + #)`. The problem is that such "naked" pound functions are ambiguous; since `#` by itself would mean `a => a`, we don't know if `# + #` is supposed to mean `(a => a) + (a => a)`, or `(a, b) = a + b`. In theory, we could avoid this issue by introducing the rule that any expression involving one or more `#` or `#n` is considered to be a single pound function, so `# + #` would mean `(a, b) => a + b`, as we presumably want. However, that would prevent us from using nested pound functions; if we really want to write the equivalent of `a => foo(a, b => b)`, we could no longer write `foo(#, #)`, because this would be interpreted as `(a, b) => foo(a, b)`. An extremely awkward solution to this possibly subcase of nested pound functions would be to introduce a `##` syntax, where sub-expressions in which `##` are encountered are treated as separate pound functions, allowing us to write `foo(#, ##)`. Or, allow the explicit pound function syntax in this case, making it `foo(#, #{#})`. But that is probably too cryptic for even the most avid syntax hackers. Bob