Skip to content

Commit

Permalink
Added rough strawman for functional operators
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jul 20, 2017
0 parents commit 326f966
Show file tree
Hide file tree
Showing 15 changed files with 5,184 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"rbuckton.grammarkdown-language",
"rbuckton.ecmarkup-vscode"
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.html": "ecmarkup"
}
}
9 changes: 9 additions & 0 deletions .yo-rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"generator-ecmascript-proposal": {
"promptValues": {
"stage": 0,
"authorName": "Ron Buckton",
"authorEmail": "[email protected]"
}
}
}
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2017, Ron Buckton, Ecma International
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156 changes: 156 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<!--
Welcome to your new proposal repository. This document will serve as the introduction and
strawman for your proposal.
The repository is broken down into the following layout:
/README.md # intro/strawman (this file)
/LICENSE # ECMA compatible license (BSD-3 Clause)
/src # ecmarkup sources for the specification
/docs # ecmarkup output
To build the specification, run:
npm run compile
To preview the specification, run:
npm run start
It is recommended that you configure GitHub Pages in your GitHub repository to point to the
'/docs' directory after you push these changes to 'master'. That way the specification text
will be updated automatically when you publish.
-->

# Proposal to add functional operators (`{+}`, `{-}`) to ECMAScript

This strawman seeks to define the possible syntax and semantics for functional operators
for ECMAScript.

## Status

**Stage:** 0
**Champion:** _None identified_

_For more information see the [TC39 proposal process](https://tc39.github.io/process-document/)._

<!-- The following sections are optional and may be uncommented if needed: --->

<!-- # Motivations -->
<!-- Motivations and use cases for the proposal --->

# Proposal

A "functional operator" is an operator-like sigil that acts like a function at runtime. In
addition, a "binary functional operator" allows either the first or second operand to be fixed.

Most functional operators are written as `{<<op>>}` where `<<op>>` is one of the binary or
unary punctuators in JavaScript, or the `instanceof`, `in`, and `typeof` keywords. In cases
where an operator has an overloaded definition between both binary and unary forms, the unary
versions of the operator have the form `{~<<op>>}`.

Each functional operator can be expressed as existing JavaScript given the following syntactic
conversions:

```js
// exponentiation operator function
{**} // (a, b) => a ** b

// multiplicative operator function
{*} // (a, b) => a * b
{/} // (a, b) => a / b
{%} // (a, b) => a % b

// additive operator function
{+} // (a, b) => a + b
{-} // (a, b) => a - b

// shift operator function
{<<} // (a, b) => a << b
{>>} // (a, b) => a >> b
{>>>} // (a, b) => a >>> b

// relational operator function
{<} // (a, b) => a < b
{<=} // (a, b) => a <= b
{>} // (a, b) => a > b
{>=} // (a, b) => a >= b
{instanceof} // (a, b) => a instanceof b
{in} // (a, b) => a in b

// equality operator function
{==} // (a, b) => a == b
{===} // (a, b) => a === b
{!=} // (a, b) => a != b
{!==} // (a, b) => a !== b

// bitwise operator function
{&} // (a, b) => a & b
{|} // (a, b) => a | b
{^} // (a, b) => a ^ b

// logical operator function
{&&} // (a, b) => a && b
{||} // (a, b) => a || b

// unary additive operator function
{~+} // (a) => +a
{~-} // (a) => -a

// unary bitwise operator function
{~} // (a) => ~a

// unary logical operator function
{!} // (a) => !a

// other unary operator function
(typeof) // (a) => typeof a
```
Each functional operator is a frozen function that exists at most once per realm. This can help to
cut down on the number of function objects allocated within a given program.
## Fixed arguments
In addition, binary functional operators may fix either the first or second operand:
```js
// fixed arguments
{+} 1 // (a) => a + 1

2 {*} // (a) => 2 * a
```
In these cases, the function returned is a unique frozen function object.
# Examples
```js
const sum = ar.reduce({+});

const result = numbers
.map({*} 2) // scale
.map({+} 5) // offset

const strings = numbers
.map({+} "");

const numbers = strings
.map({~+});

const positive = numbers
.filter({>} 0);

const halves = numbers
.map({/} 2); // no need for regexp lookahead/cover grammar
```
<!-- # Grammar -->
<!-- Grammar for the proposal. Please use grammarkdown (github.com/rbuckton/grammarkdown#readme) syntax in fenced code blocks. -->
<!-- # Semantics -->
<!-- Static and runtime semantics of the proposal -->
<!-- # References -->
<!-- Links to other specifications, prior art, etc. -->
Loading

0 comments on commit 326f966

Please sign in to comment.