functionalscript 0.0.251 → 0.0.264
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 +135 -112
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,158 +1,181 @@
|
|
|
1
1
|
# FunctionalScript
|
|
2
2
|
|
|
3
|
-
FunctionalScript is a pure functional programming language and a strict subset of
|
|
3
|
+
FunctionalScript is a pure functional programming language and a strict subset of
|
|
4
|
+
[ECMAScript](https://en.wikipedia.org/wiki/ECMAScript)/[JavaScript](https://en.wikipedia.org/wiki/JavaScript). It's inspired by
|
|
4
5
|
|
|
5
|
-
- [JSON](https://en.wikipedia.org/wiki/JSON)
|
|
6
|
-
- [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.
|
|
7
8
|
- [TypeScript](https://en.wikipedia.org/wiki/TypeScript), as a superset of JavaScript.
|
|
8
9
|
|
|
10
|
+
Create a new FunctionalScript repository on GitHub [here](https://github.com/functionalscript/template/generate).
|
|
11
|
+
|
|
9
12
|
Try FunctionalScript [here](https://functionalscript.com/).
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## 1. Design Principles
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
A workaround for this problem is to use `let` for renaming objects.
|
|
16
|
+
In FunctionalScript:
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
- Any module is a valid JavaScript module
|
|
19
|
+
- Code should not have [side-effects](https://en.wikipedia.org/wiki/Side_effect_(computer_science)). Any JavaScript statement, expression, or function which has a side effect is not allowed in FunctionalScript. There are no exceptions to this rule, such as `unsafe` code which can be found in Rust, C#, and other languages.
|
|
20
|
+
- A module can't depend on non FunctionalScript module.
|
|
21
|
+
- It also has no standard library, only a safe subset of standard JavaScript API can be used without referencing other modules.
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
npm install -S github:functionalscript/functionalscript
|
|
20
|
-
```
|
|
23
|
+
## 2. Outlines
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
### 2.1. Module Ecosystem
|
|
26
|
+
|
|
27
|
+
FunctionalScript uses [CommonJS](https://en.wikipedia.org/wiki/CommonJS) conventions as a module ecosystem. For example,
|
|
23
28
|
|
|
24
29
|
```js
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
array = '[' (() | items) ']'
|
|
29
|
-
items = expression (() | ',' items)
|
|
30
|
-
object = '{' (() | properties) '}'
|
|
31
|
-
properties = propertyId ':' expression (() | ',' properties)
|
|
32
|
-
propertyId = string
|
|
30
|
+
const thirdPartyModule = require('third-party-package/module')
|
|
31
|
+
|
|
32
|
+
const result = thirdPartyModule.someFunction('hello')
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
### 2.2. Packages
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
FunctionalScript uses a `package.json` file to define a package. This file is compatible with [Node.js `package.json`](https://nodejs.org/en/knowledge/getting-started/npm/what-is-the-file-package-json/).
|
|
38
|
+
The prefered way to refence dependencies is to use a GitHub URL. These dependencies in a `package.json` file could look like this,
|
|
38
39
|
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
ifStatement = `if` `(` expression `)` body
|
|
48
|
-
propertyAccessor = expression `[` expression `]`
|
|
49
|
-
call = expression `(` ( expression | ()) `)`
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
...
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"third-party-package": "github:exampleorg/thirdpartypackage"
|
|
45
|
+
}
|
|
46
|
+
...
|
|
47
|
+
}
|
|
50
48
|
```
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
**Note:** this repository is also a FunctionalScript package, and it can be used as a library. To install this package, use
|
|
53
51
|
|
|
54
|
-
```
|
|
55
|
-
|
|
52
|
+
```
|
|
53
|
+
npm install -S github:functionalscript/functionalscript
|
|
56
54
|
```
|
|
57
55
|
|
|
58
|
-
###
|
|
56
|
+
### 2.3. Module Structure
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
A module is a file with the `.js` extention. It contains three parts: references to other modules, definitions, and exports. For example
|
|
61
59
|
|
|
60
|
+
`./first.js`
|
|
62
61
|
```js
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
// 1. references
|
|
63
|
+
const math = require('math')
|
|
64
|
+
|
|
65
|
+
// 2. definitions
|
|
66
|
+
const myConst = 42
|
|
67
|
+
// addition(a)(b) = a + b
|
|
68
|
+
const addition = a => b => a + b
|
|
69
|
+
const add42 = addition(42)
|
|
70
|
+
const _10digitsOfPi = math.calculatePi(10)
|
|
71
|
+
|
|
72
|
+
// 3. exports
|
|
73
|
+
module.exports = {
|
|
74
|
+
addition,
|
|
75
|
+
add42,
|
|
76
|
+
_10digitsOfPi,
|
|
77
|
+
}
|
|
72
78
|
```
|
|
73
79
|
|
|
74
|
-
|
|
80
|
+
`./second.js`
|
|
81
|
+
```js
|
|
82
|
+
// 1. references
|
|
83
|
+
const first = require('./first.js')
|
|
84
|
+
|
|
85
|
+
const _42plus7 = first.add42(7)
|
|
86
|
+
```
|
|
75
87
|
|
|
76
|
-
|
|
88
|
+
### 2.4. References To Other Modules
|
|
77
89
|
|
|
78
|
-
|
|
90
|
+
The format of references is `const ANYNAME = require('PATH_TO_A_MODULE')`. For example,
|
|
79
91
|
|
|
80
92
|
```js
|
|
81
|
-
|
|
93
|
+
const math = require('math')
|
|
94
|
+
const algebra = require('math/algebra')
|
|
95
|
+
const localFile = require('../some-directory/some-file.js')
|
|
82
96
|
```
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
### 2.5. Definitions
|
|
85
99
|
|
|
86
|
-
|
|
87
|
-
propertyAccessor = expression (('[' expression ']') | ('.' id))
|
|
88
|
-
```
|
|
100
|
+
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).
|
|
89
101
|
|
|
90
102
|
```js
|
|
91
|
-
|
|
103
|
+
const myConst = 42
|
|
104
|
+
const functionDouble = a => a * 2
|
|
105
|
+
const structure = { name: "John", surname: "Smith" }
|
|
106
|
+
const array = [1, 2, 3]
|
|
107
|
+
const nestedStructure = {
|
|
108
|
+
address: undefined,
|
|
109
|
+
serialNumber: "123-45-78",
|
|
110
|
+
sum: 14 + myConst + functionDouble(4),
|
|
111
|
+
moreInfo: {
|
|
112
|
+
name: "Ivan",
|
|
113
|
+
surname: "Terrible",
|
|
114
|
+
}
|
|
115
|
+
}
|
|
92
116
|
```
|
|
93
117
|
|
|
94
|
-
|
|
118
|
+
See [3. Expressions](#3-Expressions).
|
|
95
119
|
|
|
96
|
-
|
|
120
|
+
### 2.6. Exports
|
|
97
121
|
|
|
98
|
-
|
|
122
|
+
The format of exports is `module.exports = { A_LIST_OF_EXPORTED_DEFINITIONS }`. There should be only one `module.exports` at
|
|
123
|
+
the end of a FunctionalScript file. For example,
|
|
99
124
|
|
|
100
125
|
```js
|
|
101
|
-
|
|
102
|
-
|
|
126
|
+
module.exports = {
|
|
127
|
+
nestedStructure,
|
|
128
|
+
array,
|
|
129
|
+
structure,
|
|
130
|
+
}
|
|
103
131
|
```
|
|
104
132
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
133
|
+
## 3. Expressions
|
|
134
|
+
|
|
135
|
+
Expressions could fall under these categories:
|
|
136
|
+
|
|
137
|
+
- Literals:
|
|
138
|
+
- Number Literals, e.g. `0`, `3.14`, `4e8`
|
|
139
|
+
- Boolean Literals: `true` or `false`
|
|
140
|
+
- A `null` Literal
|
|
141
|
+
- An `undefined` Literal
|
|
142
|
+
- String Literals, e.g. `"Hello world!"`
|
|
143
|
+
- Complex Structures
|
|
144
|
+
- Arrays, e.g. `[2, 5]`
|
|
145
|
+
- Objects, e.g. `{ a: "Hello", b: "world!" }`
|
|
146
|
+
- Arrow functions, e.g. `x => x * 2`
|
|
147
|
+
- Operators
|
|
148
|
+
- Comparison Operators: `===`, `!==`, `>`, `>=`, `<`, `<=`
|
|
149
|
+
- Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `**`
|
|
150
|
+
- Bitwise Operators: `&`, `|`, `^`, `~`, `<<`, `>>`, `>>>`
|
|
151
|
+
- Logical Operators: `&&`, `||`, `!`, `??`
|
|
152
|
+
- Conditional Operator, e.g. `condition ? val1 : val2`
|
|
153
|
+
- Template Literals, e.g. `string ${expression}`
|
|
154
|
+
- `typeof`
|
|
155
|
+
- Relations Operators: `in`, `instanceof`.
|
|
156
|
+
- Member Operators: `.`, `[]`.
|
|
157
|
+
|
|
158
|
+
Note: the `.` member operator has prohibitted property names, such as `constructor` and `push`. To access such properties, it's recommeded to use the `Object.getPropertyDescriptor` function.
|
|
159
|
+
|
|
160
|
+
## 4. Arrow Functions
|
|
161
|
+
|
|
162
|
+
An arrow function is also known as [a lambda function](https://en.wikipedia.org/wiki/Anonymous_function).
|
|
163
|
+
The format of an arrow function is `ARGUMENT_NAME => FUNCTION_BODY`. An arrow function must have either a single argument or no arguments at all. For example
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
x => x * 2
|
|
167
|
+
a => a + 4
|
|
168
|
+
s => `template literal ${s}`
|
|
169
|
+
() => 'hello' // an arrow function with no arguments
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
A function body is either an expression or a block statement. A block statement format is `{ A_LIST_OF_STATEMENTS }`. For example
|
|
173
|
+
|
|
117
174
|
```js
|
|
118
|
-
|
|
119
|
-
|
|
175
|
+
// a function with one argument and a block statement
|
|
176
|
+
const f = x => {
|
|
177
|
+
const a = 2 + x
|
|
178
|
+
const r = a + 4
|
|
179
|
+
return r
|
|
180
|
+
}
|
|
120
181
|
```
|
|
121
|
-
Regular expressions.
|
|
122
|
-
|
|
123
|
-
## Stage 1
|
|
124
|
-
|
|
125
|
-
Typing using [JSDoc](https://jsdoc.app/) and TypeScript types.
|
|
126
|
-
|
|
127
|
-
## Stage 2
|
|
128
|
-
|
|
129
|
-
Mutable types with exclusive ownership (similar to Rust mutability).
|
|
130
|
-
|
|
131
|
-
- `let`, `for`, `while` etc.
|
|
132
|
-
Note: `let` can work as an object name reuse.
|
|
133
|
-
In this case, `let` objects can't be used in nested functions. It means we can't reference `let` object.
|
|
134
|
-
```js
|
|
135
|
-
let x = 5 // ok
|
|
136
|
-
f(x)
|
|
137
|
-
x = 'hello!' // ok
|
|
138
|
-
f(x)
|
|
139
|
-
const r = () => {
|
|
140
|
-
return x // compilation error
|
|
141
|
-
}
|
|
142
|
-
```
|
|
143
|
-
Translated into
|
|
144
|
-
```js
|
|
145
|
-
const x0 = 5
|
|
146
|
-
f(x0)
|
|
147
|
-
const x1 = 'hello!'
|
|
148
|
-
f(x1)
|
|
149
|
-
```
|
|
150
|
-
- Generators `function*(){ ... yield ... }`.
|
|
151
|
-
- Async `async () => f(await exp())`.
|
|
152
|
-
- hopefully, we will have [ES pipe operator](https://tc39.es/proposal-pipeline-operator/) at this time.
|
|
153
|
-
- [pattern matching](https://github.com/tc39/proposal-pattern-matching)
|
|
154
|
-
|
|
155
|
-
Controversial ideas:
|
|
156
|
-
|
|
157
|
-
- Import and export `import x from "..."`, `export const x = ...`, `export default = ` e.t.c. This may break `new Function` runners.
|
|
158
|
-
- Functional-TypeScript as a subset of TypeScript. Note: FunctionalScript doesn't require an additional build step in contrast to TypeScript.
|