Swift style syntax

Mohsen Azimi me at azimi.me
Mon Oct 12 17:20:53 UTC 2015


> reversed = names.sort(>)
> This might actually be possible - I can't think of any ambiguous
situations for passing operators as if they were first class functions. If
it is possible, I'd like to see this done.

Yes, it would be really cool if operators can be used as function. For
example in Swift you can do this:

```
let scores = [10, 13, 15, 8, 9, 19, 20, 4, 6];

let sum = scores.reduce(0, combine: +) // 104
```

Or this:

```
var bools = [true, false, false, true, true];

bools.filter(!) // [false, false]
```


On Mon, Oct 12, 2015 at 7:29 AM Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> for "historical record" sake that silly trick works even in a more
> meaningful way with RegExp replacements  :-)
>
> ```js
> // before
> 'str'.replace(/(some)(thing)/, function ($0, $1, $2) {
>   //  boring $1 $2 like RegExp.$1 and  RegExp.$2
> });
>
> // now
> 'str'.replace(/(some)(thing)/, (...$) => {
>   // we have $[1], $[2] ... yaiiii
> });
> ```
>
> Regards
>
>
>
> On Mon, Oct 12, 2015 at 7:40 AM, Isiah Meadows <isiahmeadows at gmail.com>
> wrote:
>
>> Interesting trick, Andrea. Never thought of that before.
>>
>> On Mon, Oct 12, 2015, 02:31 Andrea Giammarchi <
>> andrea.giammarchi at gmail.com> wrote:
>>
>>> ... sort of (no  pun intended)
>>>
>>> ```js
>>> let sorted = names.sort((...$) => $[0] > $[1]);
>>> ```
>>>
>>> Regards
>>>
>>> On Mon, Oct 12, 2015 at 2:58 AM, Frankie Bagnardi <f.bagnardi at gmail.com>
>>> wrote:
>>>
>>>> I don't think there's much value in this. Also sort is a bad example
>>>> because it'd look like this, and there's no nifty shortcut answer to it.
>>>>
>>>> ```js
>>>> names.sort((a, b) => a < b ? 1 : a > b ? -1 : 0);
>>>> ```
>>>>
>>>> In most cases you save a couple characters, but you can just use
>>>> x/y/a/b/f/g/n/xs/xss for variable names in arrow functions instead of the
>>>> $0 (which would likely be \0 in js).
>>>>
>>>>
>>>>
>>>> On Sun, Oct 11, 2015 at 3:26 PM, Caitlin Potter <caitpotter88 at gmail.com
>>>> > wrote:
>>>>
>>>>> In the case of sorting, are arrow functions not good enough? Or are we
>>>>> really asking for full continuation support
>>>>>
>>>>> On Oct 11, 2015, at 5:51 PM, Alexander Jones <alex at weej.com> wrote:
>>>>>
>>>>> IMO this is a good idea. When it's abundantly clear from context, I've
>>>>> already been naming my arrow function params _ if singular, and _1, _2 etc
>>>>> if several. As always, picking some punctuation straight from the scarce
>>>>> and sacred set of remaining ASCII symbols is going to be tricky. (If only
>>>>> we could just go APL on this!)
>>>>>
>>>>> On 11 October 2015 at 16:45, Mohsen Azimi <me at azimi.me> wrote:
>>>>>
>>>>>> Is it possible to extend JavaScript syntax to support Swift style
>>>>>> block syntax[1]?
>>>>>>
>>>>>> In Swift it's possible to omit return keyword
>>>>>> ```
>>>>>>
>>>>>>    1. reversed = names.sort( { s1, s2 in s1 > s2 } )
>>>>>>
>>>>>> ```
>>>>>>
>>>>>> or omit argument declaration like this:
>>>>>>
>>>>>> ```
>>>>>>
>>>>>>    1. reversed = names.sort( { $0 > $1 } )
>>>>>>
>>>>>> ```
>>>>>>
>>>>>> or apply an operator to arguments of a function
>>>>>>
>>>>>> ```
>>>>>>
>>>>>>    1. reversed = names.sort(>)
>>>>>>
>>>>>> ```
>>>>>> We have the first feature in ES2015 already:
>>>>>>
>>>>>> ```
>>>>>> let sorted = names.sort((a, b)=> a > b);
>>>>>> ```
>>>>>>
>>>>>> But for omitting argument declaration we need to find an alternative
>>>>>> to $0, $1... since those are valid variable names in JS. Maybe we can use
>>>>>> #0, #1... instead.
>>>>>>
>>>>>> This is very useful for functional programming aspect of JS. For
>>>>>> example in a filter function:
>>>>>>
>>>>>> ```
>>>>>> let passed = objs.filter(#0.passed)
>>>>>> ```
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> [1][
>>>>>> https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
>>>>>> ]
>>>>>>
>>>>>> _______________________________________________
>>>>>> es-discuss mailing list
>>>>>> es-discuss at mozilla.org
>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>
>>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> es-discuss mailing list
>>>> es-discuss at mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151012/1e1f4b4f/attachment-0001.html>


More information about the es-discuss mailing list