Swift style syntax
Ates Goral
ates at magnetiq.com
Tue May 7 04:58:21 UTC 2019
This topic has been discussed a long time ago and has stalled on
discussions of a standard library.
Coming back to the idea of adding some basic math operations directly
on Math, there could be value in adding at least the following (from
Isiah Meadows's post):
> - `a + b` -> `Math.add` -> `:+`
> - `a - b` -> `Math.sub` -> `:-`
> - `a * b` -> `Math.mul` -> `:*` (different from `imul`)
> - `a / b` -> `Math.div` -> `:/`
`Math.add`: Among potentially other things, this could be useful for
an out-of-the-box, terse way to add up items in an array. So, instead
of:
```
array.reduce((sum, item) => sum + item);
```
Items could be added up with:
```
array.reduce(Math.add);
```
`Math.sub`: Among potentially other things, this could be useful for
sorting numbers without having to define a custom comparator (i.e. almost as
an out-of-the-box riposte to the "JavaScript WTF" refrain of
lexicographically sorting numbers by using the default sort
implementation.) So, instead of:
```
array.sort((first, second) => first - second);
```
Numbers could be sorted with:
```
array.sort(Math.sub);
```
`Math.mul` and `Math.div`: I don't have a compelling use case in mind,
but I'd just throw these in for good measure / symmetry.
Ates
More information about the es-discuss
mailing list