functionalscript 0.0.254 → 0.0.258
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -113
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,158 +3,126 @@
|
|
|
3
3
|
FunctionalScript is a pure functional programming language and a strict subset of
|
|
4
4
|
[ECMAScript](https://en.wikipedia.org/wiki/ECMAScript)/[JavaScript](https://en.wikipedia.org/wiki/JavaScript). It's inspired by
|
|
5
5
|
|
|
6
|
-
- [JSON](https://en.wikipedia.org/wiki/JSON), as a subset of JavaScript
|
|
7
|
-
- [asm.JS](https://en.wikipedia.org/wiki/Asm.js)/[WebAssembly](https://en.wikipedia.org/wiki/WebAssembly), as a subset of JavaScript
|
|
6
|
+
- [JSON](https://en.wikipedia.org/wiki/JSON), as a subset of JavaScript. JSON is also a subset of FunctionalScript.
|
|
7
|
+
- [asm.JS](https://en.wikipedia.org/wiki/Asm.js)/[WebAssembly](https://en.wikipedia.org/wiki/WebAssembly), as a subset of JavaScript.
|
|
8
8
|
- [TypeScript](https://en.wikipedia.org/wiki/TypeScript), as a superset of JavaScript.
|
|
9
9
|
|
|
10
10
|
Try FunctionalScript [here](https://functionalscript.com/).
|
|
11
11
|
|
|
12
12
|
Create a new FunctionalScript repository on GitHub [here](https://github.com/functionalscript/template/generate).
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
A workaround for this problem is to use `let` for renaming objects.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
## Install FunctionalScript As A Library
|
|
14
|
+
To install this repository as a library use:
|
|
19
15
|
|
|
20
16
|
```
|
|
21
17
|
npm install -S github:functionalscript/functionalscript
|
|
22
18
|
```
|
|
23
19
|
|
|
24
|
-
##
|
|
20
|
+
## 1. Design Principles
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
jsonFile = expression
|
|
28
|
-
expression = primitive | array | objects
|
|
29
|
-
primitive = 'true' | 'false' | 'null' | number | string
|
|
30
|
-
array = '[' (() | items) ']'
|
|
31
|
-
items = expression (() | ',' items)
|
|
32
|
-
object = '{' (() | properties) '}'
|
|
33
|
-
properties = propertyId ':' expression (() | ',' properties)
|
|
34
|
-
propertyId = string
|
|
35
|
-
```
|
|
22
|
+
In FunctionalScript:
|
|
36
23
|
|
|
37
|
-
|
|
24
|
+
- Any module is a valid JavaScript module
|
|
25
|
+
- A module can't depend on non FunctionalScript module.
|
|
26
|
+
- A module can contain only pure functional statements. There are no exceptions to this rule, such as `unsafe` code in Rust or C#.
|
|
27
|
+
- It also has no standard library, only a safe subset of standard JavaScript API is allowed.
|
|
38
28
|
|
|
39
|
-
|
|
29
|
+
## 2. Outlines
|
|
40
30
|
|
|
41
|
-
|
|
42
|
-
fjsFile = expression
|
|
43
|
-
expression = primitive | array | object | func | id | propertyAccessor
|
|
44
|
-
func = ('()' | id) '=>' body
|
|
45
|
-
body = '{' statements 'return' expression ';' '}'
|
|
46
|
-
statements = () | (statement statements)
|
|
47
|
-
statement = decl | ifStatement
|
|
48
|
-
decl = `const` id `=` expression `;`
|
|
49
|
-
ifStatement = `if` `(` expression `)` body
|
|
50
|
-
propertyAccessor = expression `[` expression `]`
|
|
51
|
-
call = expression `(` ( expression | ()) `)`
|
|
52
|
-
```
|
|
31
|
+
### 2.1. Module Ecosystem
|
|
53
32
|
|
|
54
|
-
|
|
33
|
+
FunctionalScript uses Common.JS conventions as a module ecosystem. For example,
|
|
55
34
|
|
|
56
35
|
```js
|
|
57
|
-
|
|
36
|
+
const thirdPartyModule = require('third-party-package/module')
|
|
37
|
+
|
|
38
|
+
const result = thirdPartyModule.someFunction('hello')
|
|
58
39
|
```
|
|
59
40
|
|
|
60
|
-
###
|
|
41
|
+
### 2.2. Packages
|
|
61
42
|
|
|
62
|
-
|
|
43
|
+
FunctionalScript uses a `package.json` file to define a package. This file is compatible with Node.js `package.json`.
|
|
44
|
+
The prefered way to refence dependencies is to use a GitHub URL. An example of dependencies in a `package.json` file:
|
|
63
45
|
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
logicalBinaryOperator = '&&' | '||'
|
|
73
|
-
unaryOperator = '-' | '~' | '!'
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
...
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"third-party-package": "github:exampleorg/thirdpartypackage"
|
|
51
|
+
}
|
|
52
|
+
...
|
|
53
|
+
}
|
|
74
54
|
```
|
|
75
55
|
|
|
76
|
-
|
|
56
|
+
### 2.2. Module Structure
|
|
77
57
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
#### Function Expression
|
|
58
|
+
A module is a file with the `.js` extention. It contains three parts: references to other modules, definitions, and exports. For example
|
|
81
59
|
|
|
60
|
+
`./first.js`
|
|
82
61
|
```js
|
|
83
|
-
|
|
62
|
+
// 1. references
|
|
63
|
+
const math = require('math')
|
|
64
|
+
|
|
65
|
+
// 2. definitions
|
|
66
|
+
const myConst = 42
|
|
67
|
+
const addition = a => b => a + b
|
|
68
|
+
const add42 = addition(42)
|
|
69
|
+
const _10digitsOfPi = math.calculatePi(10)
|
|
70
|
+
|
|
71
|
+
// 3. exports
|
|
72
|
+
module.exports = {
|
|
73
|
+
addition,
|
|
74
|
+
add42,
|
|
75
|
+
_10digitsOfPi,
|
|
76
|
+
}
|
|
84
77
|
```
|
|
85
78
|
|
|
86
|
-
|
|
87
|
-
|
|
79
|
+
`./second.js`
|
|
88
80
|
```js
|
|
89
|
-
|
|
81
|
+
// 1. references
|
|
82
|
+
const first = require('./first.js')
|
|
83
|
+
|
|
84
|
+
const _42plus7 = first.add42(7)
|
|
90
85
|
```
|
|
91
86
|
|
|
87
|
+
### 2.3. References To Other Modules
|
|
88
|
+
|
|
89
|
+
The format of references is `const ANYNAME = require('PATH_TO_A_MODULE')`. For example
|
|
90
|
+
|
|
92
91
|
```js
|
|
93
|
-
|
|
92
|
+
const math = require('math')
|
|
93
|
+
const algebra = require('math/algebra')
|
|
94
|
+
const localFile = require('../some-directory/some-file.js')
|
|
94
95
|
```
|
|
95
96
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
For example `42n`.
|
|
97
|
+
### 2.4. Definitions
|
|
99
98
|
|
|
100
|
-
|
|
99
|
+
The format of defintions is `const NAME = EXPRESSION`, where the `EXPRESSION` is a subset of [JavaScript expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators).
|
|
101
100
|
|
|
102
101
|
```js
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
const myConst = 42
|
|
103
|
+
const functionDouble = a => a * 2
|
|
104
|
+
const structure = { name: "John", surname: "Smith" }
|
|
105
|
+
const array = [1, 2, 3]
|
|
106
|
+
const nestedStructure = {
|
|
107
|
+
address: undefined,
|
|
108
|
+
serialNumber: "123-45-78",
|
|
109
|
+
sum: 14 + myConst + functionDouble(4),
|
|
110
|
+
moreInfo: {
|
|
111
|
+
name: "Ivan",
|
|
112
|
+
surname: "Terrible",
|
|
113
|
+
}
|
|
114
|
+
}
|
|
105
115
|
```
|
|
106
116
|
|
|
107
|
-
###
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
Destructing assignments. For example `const {a,b} = exp;`, `const [a, b] = exp`.
|
|
113
|
-
Property Id expression `{ [exp]: exp }`.
|
|
114
|
-
Allow no semicolons.
|
|
115
|
-
Optional comma in arrays and objects.
|
|
116
|
-
Template literals ``const r= `onst r = ${exp}`;``.
|
|
117
|
-
An `if` statement `if (exp) { ... return exp }`
|
|
118
|
-
Multiline strings
|
|
117
|
+
### 2.5. Exports
|
|
118
|
+
|
|
119
|
+
The format of exports is `module.exports = { A_LIST_OF_EXPORTED_DEFINITIONS }`. There should be only one `module.exports` at
|
|
120
|
+
the end of a FunctionalScript file. For example
|
|
121
|
+
|
|
119
122
|
```js
|
|
120
|
-
|
|
121
|
-
|
|
123
|
+
module.exports = {
|
|
124
|
+
nestedStructure,
|
|
125
|
+
array,
|
|
126
|
+
structure,
|
|
127
|
+
}
|
|
122
128
|
```
|
|
123
|
-
Regular expressions.
|
|
124
|
-
|
|
125
|
-
## Stage 1
|
|
126
|
-
|
|
127
|
-
Typing using [JSDoc](https://jsdoc.app/) and TypeScript types.
|
|
128
|
-
|
|
129
|
-
## Stage 2
|
|
130
|
-
|
|
131
|
-
Mutable types with exclusive ownership (similar to Rust mutability).
|
|
132
|
-
|
|
133
|
-
- `let`, `for`, `while` etc.
|
|
134
|
-
Note: `let` can work as an object name reuse.
|
|
135
|
-
In this case, `let` objects can't be used in nested functions. It means we can't reference `let` object.
|
|
136
|
-
```js
|
|
137
|
-
let x = 5 // ok
|
|
138
|
-
f(x)
|
|
139
|
-
x = 'hello!' // ok
|
|
140
|
-
f(x)
|
|
141
|
-
const r = () => {
|
|
142
|
-
return x // compilation error
|
|
143
|
-
}
|
|
144
|
-
```
|
|
145
|
-
Translated into
|
|
146
|
-
```js
|
|
147
|
-
const x0 = 5
|
|
148
|
-
f(x0)
|
|
149
|
-
const x1 = 'hello!'
|
|
150
|
-
f(x1)
|
|
151
|
-
```
|
|
152
|
-
- Generators `function*(){ ... yield ... }`.
|
|
153
|
-
- Async `async () => f(await exp())`.
|
|
154
|
-
- hopefully, we will have [ES pipe operator](https://tc39.es/proposal-pipeline-operator/) at this time.
|
|
155
|
-
- [pattern matching](https://github.com/tc39/proposal-pattern-matching)
|
|
156
|
-
|
|
157
|
-
Controversial ideas:
|
|
158
|
-
|
|
159
|
-
- Import and export `import x from "..."`, `export const x = ...`, `export default = ` e.t.c. This may break `new Function` runners.
|
|
160
|
-
- Functional-TypeScript as a subset of TypeScript. Note: FunctionalScript doesn't require an additional build step in contrast to TypeScript.
|