new feature proposal for anonymous functions

54 views
Skip to first unread message

Alexander Tsepkov

unread,
Sep 17, 2015, 1:06:58 AM9/17/15
to RapydScript
Since JavaScript encourages use of callbacks and a lot of design patterns (both in JavaScript and RapydScript) rely on them (map, filter, forEach, sort, etc.), there are multiple situations that require a one-liner functions, akin to Python's lambdas (which I don't like because they introduce an unnecessary keyword). To make code less verbose I propose a new feature/convention:

if and only if the function consists of a single statement and is anonymous, the return is implicit. That means the following no longer needs a return:

    { "add": def(a, b): a+b }

This also means we still preserve the sanity of Python rather than having to deal with special cases created by implicit return that have been biting CoffeeScript community (https://github.com/jashkenas/coffeescript/issues/2477, https://github.com/jashkenas/coffeescript/issues/4041, https://github.com/jashkenas/coffeescript/pull/3731). To avoid an implicit return you can simply rewrite the above function as follows:

    def(a, b):
        a + b
        pass

Admittedly the function then becomes useless, but you get the idea.

Bruce Sherwood

unread,
Sep 19, 2015, 9:46:29 PM9/19/15
to RapydScript
I really don't like the implicit return in CoffeeScript, which is confusing and problematic. I would MUCH prefer having to write { "add": def(a,b): return a+b }. I don't see any advantage at all to avoid saying "return".

Bruce

Alexander Tsepkov

unread,
Sep 21, 2015, 12:59:12 PM9/21/15
to Bruce Sherwood, RapydScript
I'm not planning to support implicit returns the same way CoffeeScript does. The feature would only be allowed for single-statement function, even something as simple as adding a pass/comment would invalidate this. I don't understand how this could result in confusing behavior that CoffeeScript suffers from. You can still choose to include the return (it's optional, not invalid), Python's lambda function omits return for the same reason. Unlike Python, RS doesn't have lamdas. And unlike Python, JavaScript uses callbacks A LOT, even something as simple as sort() requires a callback to avoid the senseless default behavior of sorting by digits:

[1,2,10].sort() => [1,10,2]
[1,2,10].sort(def(a, b): return a - b;) => [1,2,10]

Moreover, ES6 already plans to introduce the arrow-function syntax (https://github.com/lukehoban/es6features#arrows) which would work the same way:
// Expression bodies
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var pairs = evens.map(v => ({even: v, odd: v + 1}));

Bruce Sherwood

unread,
Sep 21, 2015, 1:31:21 PM9/21/15
to Alexander Tsepkov, RapydScript
I see. Thanks for the further explanation.

Bruce

Steve Theodore

unread,
Oct 5, 2015, 11:54:52 PM10/5/15
to RapydScript
I know you're don't like lambda, but that would disambiguate the function-with-explicit return case from the function-with-implicit-return case pretty straightforwardly. It would also make it much easier to port generic python code.  

Maybe if you used the => syntax and allowed lambda as syntax sugar, so

example = lambda p:p+1

 and

example = p => p+1

compiled the same?
Reply all
Reply to author
Forward
0 new messages