@mojir/lits 2.1.32 → 2.1.33
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 +34 -69
- package/dist/cli/cli.js +211 -333
- package/dist/cli/src/builtin/index.d.ts +1 -1
- package/dist/cli/src/builtin/specialExpressionTypes.d.ts +13 -15
- package/dist/cli/src/builtin/specialExpressions/functions.d.ts +3 -7
- package/dist/cli/src/builtin/specialExpressions/loop.d.ts +1 -1
- package/dist/cli/src/parser/Parser.d.ts +5 -4
- package/dist/cli/src/tokenizer/reservedNames.d.ts +2 -0
- package/dist/index.esm.js +220 -342
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +220 -342
- package/dist/index.js.map +1 -1
- package/dist/lits.iife.js +220 -342
- package/dist/lits.iife.js.map +1 -1
- package/dist/src/builtin/index.d.ts +1 -1
- package/dist/src/builtin/specialExpressionTypes.d.ts +13 -15
- package/dist/src/builtin/specialExpressions/functions.d.ts +3 -7
- package/dist/src/builtin/specialExpressions/loop.d.ts +1 -1
- package/dist/src/parser/Parser.d.ts +5 -4
- package/dist/src/tokenizer/reservedNames.d.ts +2 -0
- package/dist/testFramework.esm.js +210 -332
- package/dist/testFramework.esm.js.map +1 -1
- package/dist/testFramework.js +210 -332
- package/dist/testFramework.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@ Try it in the [Lits Playground](https://mojir.github.io/lits/).
|
|
|
10
10
|
- **JavaScript interoperability** - JavaScript values and functions can easily be exposed in Lits
|
|
11
11
|
- **First-class functions** - Functions are treated as values that can be passed to other functions
|
|
12
12
|
- **Algebraic notation** - All operators can be used as functions, and functions that take two parameters can be used as operators
|
|
13
|
-
- **Clojure-inspired functions** - Most core functions are inspired by Clojure
|
|
14
13
|
- **Comprehensive standard library** - Rich set of functions for collections, math, strings, and more
|
|
15
14
|
- **Structural equality** - Objects are compared by value, not by reference
|
|
16
15
|
- **Destructuring** - Extract values from complex data structures with ease
|
|
@@ -25,20 +24,18 @@ Here's a simple example to get you started:
|
|
|
25
24
|
|
|
26
25
|
```
|
|
27
26
|
// Defining a function
|
|
28
|
-
|
|
29
|
-
x * x
|
|
30
|
-
end;
|
|
27
|
+
square = x -> x * x;
|
|
31
28
|
|
|
32
29
|
// Using the function
|
|
33
|
-
let result
|
|
30
|
+
let result = square(5);
|
|
34
31
|
// => 25
|
|
35
32
|
|
|
36
33
|
// Using function as an operator
|
|
37
|
-
let squares
|
|
34
|
+
let squares = [1, 2, 3, 4, 5] map square;
|
|
38
35
|
// => [1, 4, 9, 16, 25]
|
|
39
36
|
|
|
40
37
|
// Using operator as a function
|
|
41
|
-
let sum
|
|
38
|
+
let sum = +([1, 2, 3, 4, 5]);
|
|
42
39
|
// => 15
|
|
43
40
|
```
|
|
44
41
|
|
|
@@ -69,7 +66,7 @@ null
|
|
|
69
66
|
[1, 2, 3, 4]
|
|
70
67
|
|
|
71
68
|
// Objects
|
|
72
|
-
{ name
|
|
69
|
+
{ name: "John", age: 30 }
|
|
73
70
|
```
|
|
74
71
|
|
|
75
72
|
### Builtin Number Symbols
|
|
@@ -117,15 +114,15 @@ These constants can be used anywhere a number value is expected and help make ma
|
|
|
117
114
|
|
|
118
115
|
```
|
|
119
116
|
// Let expression
|
|
120
|
-
let x
|
|
117
|
+
let x = 10;
|
|
121
118
|
// => 10
|
|
122
119
|
|
|
123
120
|
// Variables are immutable
|
|
124
|
-
let x
|
|
121
|
+
let x = 20; // Error: x is already defined
|
|
125
122
|
|
|
126
123
|
// But can be shadowed in inner scopes
|
|
127
|
-
let y
|
|
128
|
-
let x
|
|
124
|
+
let y = do
|
|
125
|
+
let x = 20;
|
|
129
126
|
x
|
|
130
127
|
end;
|
|
131
128
|
// => 20, outer x is still 10
|
|
@@ -134,20 +131,15 @@ end;
|
|
|
134
131
|
### Functions
|
|
135
132
|
|
|
136
133
|
```
|
|
137
|
-
// Standard function definition
|
|
138
|
-
function add(a, b)
|
|
139
|
-
a + b
|
|
140
|
-
end;
|
|
141
|
-
|
|
142
134
|
// Lambda functions
|
|
143
|
-
let add
|
|
135
|
+
let add = (a, b) -> a + b;
|
|
144
136
|
|
|
145
137
|
// Short form with positional arguments
|
|
146
|
-
let add
|
|
138
|
+
let add = -> $1 + $2;
|
|
147
139
|
|
|
148
140
|
// Single argument short form
|
|
149
|
-
let cube
|
|
150
|
-
let fourth
|
|
141
|
+
let cube = x -> x ** 3;
|
|
142
|
+
let fourth = -> $ ** 4;
|
|
151
143
|
```
|
|
152
144
|
|
|
153
145
|
### Control Flow
|
|
@@ -158,7 +150,6 @@ if x > 10 then
|
|
|
158
150
|
"large"
|
|
159
151
|
else
|
|
160
152
|
"small"
|
|
161
|
-
end;
|
|
162
153
|
// => "large" (if x > 10) or "small" (if x <= 10)
|
|
163
154
|
|
|
164
155
|
// Unless expression (reversed if)
|
|
@@ -166,7 +157,6 @@ unless x > 10 then
|
|
|
166
157
|
"small"
|
|
167
158
|
else
|
|
168
159
|
"large"
|
|
169
|
-
end;
|
|
170
160
|
// => "small" (if x <= 10) or "large" (if x > 10)
|
|
171
161
|
|
|
172
162
|
// Switch expression
|
|
@@ -190,7 +180,6 @@ try
|
|
|
190
180
|
riskyOperation()
|
|
191
181
|
catch (error)
|
|
192
182
|
"Error: " ++ error.message
|
|
193
|
-
end;
|
|
194
183
|
// => result of riskyOperation() or error message if an exception occurs
|
|
195
184
|
```
|
|
196
185
|
|
|
@@ -198,45 +187,27 @@ end;
|
|
|
198
187
|
|
|
199
188
|
```
|
|
200
189
|
// Simple for comprehension
|
|
201
|
-
|
|
202
|
-
each x of [0, 1, 2, 3, 4, 5], let y := x * 3, while even?(y)
|
|
203
|
-
do
|
|
204
|
-
y
|
|
205
|
-
end;
|
|
206
|
-
// => [0, 6, 12]
|
|
207
|
-
|
|
208
|
-
// Multiple generators
|
|
209
|
-
for (
|
|
210
|
-
each x of [1, 2, 3]
|
|
211
|
-
each y of [1, 2, 3], when x <= y
|
|
212
|
-
z of [1, 2, 3]
|
|
213
|
-
)
|
|
214
|
-
[x, y, z]
|
|
215
|
-
end;
|
|
216
|
-
// => [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3],
|
|
217
|
-
// [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3],
|
|
218
|
-
// [3, 3, 1], [3, 3, 2], [3, 3, 3]]
|
|
190
|
+
TODO
|
|
219
191
|
```
|
|
220
192
|
|
|
221
193
|
### Destructuring
|
|
222
194
|
|
|
223
195
|
```
|
|
224
196
|
// Object destructuring
|
|
225
|
-
let { name, age }
|
|
197
|
+
let { name, age } = { name: "John", age: 30 };
|
|
226
198
|
// name => "John"
|
|
227
199
|
// age => 30
|
|
228
200
|
|
|
229
201
|
// Array destructuring
|
|
230
|
-
let [nbr1, nbr2]
|
|
202
|
+
let [nbr1, nbr2] = [1, 2, 3, 4];
|
|
231
203
|
// nbr1 => 1
|
|
232
204
|
// nbr2 => 2
|
|
233
205
|
|
|
234
206
|
// Destructuring in function parameters
|
|
235
|
-
|
|
236
|
-
name ++ " is " ++ str(age) ++ " years old"
|
|
237
|
-
end;
|
|
207
|
+
let displayPerson = ({name, age}) ->
|
|
208
|
+
name ++ " is " ++ str(age) ++ " years old";
|
|
238
209
|
|
|
239
|
-
displayPerson({ name
|
|
210
|
+
displayPerson({ name: "John", age: 30 });
|
|
240
211
|
// => "John is 30 years old"
|
|
241
212
|
```
|
|
242
213
|
|
|
@@ -270,14 +241,14 @@ All operators can be used as functions:
|
|
|
270
241
|
|
|
271
242
|
### Parameter Order
|
|
272
243
|
|
|
273
|
-
|
|
244
|
+
Lits favors subject-first parameter order:
|
|
274
245
|
|
|
275
246
|
```
|
|
276
247
|
// Lits
|
|
277
248
|
filter([1, 2, 3, 4], odd?);
|
|
278
249
|
// => [1, 3]
|
|
279
250
|
|
|
280
|
-
//
|
|
251
|
+
// Unlike for example Clojure
|
|
281
252
|
// (filter odd? [1 2 3 4])
|
|
282
253
|
```
|
|
283
254
|
|
|
@@ -331,13 +302,11 @@ You can export definitions to make them available to other modules:
|
|
|
331
302
|
|
|
332
303
|
```
|
|
333
304
|
// Exporting variables
|
|
334
|
-
export let magic-number
|
|
305
|
+
export let magic-number = 42;
|
|
335
306
|
// => 42
|
|
336
307
|
|
|
337
308
|
// Exporting functions
|
|
338
|
-
export
|
|
339
|
-
x * x
|
|
340
|
-
end;
|
|
309
|
+
export let square = x -> x * x;
|
|
341
310
|
```
|
|
342
311
|
|
|
343
312
|
## API
|
|
@@ -364,13 +333,11 @@ interface Lits {
|
|
|
364
333
|
### Factorial Function
|
|
365
334
|
|
|
366
335
|
```
|
|
367
|
-
|
|
336
|
+
let factorial = (n) ->
|
|
368
337
|
if n <= 1 then
|
|
369
338
|
1
|
|
370
339
|
else
|
|
371
|
-
n *
|
|
372
|
-
end
|
|
373
|
-
end;
|
|
340
|
+
n * self(n - 1);
|
|
374
341
|
|
|
375
342
|
factorial(5);
|
|
376
343
|
// => 120
|
|
@@ -379,13 +346,11 @@ factorial(5);
|
|
|
379
346
|
### Fibonacci Sequence
|
|
380
347
|
|
|
381
348
|
```
|
|
382
|
-
|
|
349
|
+
let fib = n ->
|
|
383
350
|
if n < 2 then
|
|
384
351
|
n
|
|
385
352
|
else
|
|
386
|
-
|
|
387
|
-
end
|
|
388
|
-
end;
|
|
353
|
+
self(n - 1) + fib(n - 2)
|
|
389
354
|
|
|
390
355
|
// Generate the first 10 Fibonacci numbers
|
|
391
356
|
range(10) map fib;
|
|
@@ -395,11 +360,11 @@ range(10) map fib;
|
|
|
395
360
|
### Working with Collections
|
|
396
361
|
|
|
397
362
|
```
|
|
398
|
-
let people
|
|
399
|
-
{ name
|
|
400
|
-
{ name
|
|
401
|
-
{ name
|
|
402
|
-
{ name
|
|
363
|
+
let people = [
|
|
364
|
+
{ name: "Alice", age: 25 },
|
|
365
|
+
{ name: "Bob", age: 30 },
|
|
366
|
+
{ name: "Charlie", age: 35 },
|
|
367
|
+
{ name: "Diana", age: 40 }
|
|
403
368
|
];
|
|
404
369
|
|
|
405
370
|
// Get all names
|
|
@@ -408,7 +373,7 @@ people map (p -> p.name);
|
|
|
408
373
|
|
|
409
374
|
// Get people older than 30
|
|
410
375
|
people filter (p -> p.age > 30);
|
|
411
|
-
// => [{ name
|
|
376
|
+
// => [{ name: "Charlie", age: 35 }, { name: "Diana", age: 40 }]
|
|
412
377
|
|
|
413
378
|
// Calculate average age
|
|
414
379
|
(people map (p -> p.age) reduce +) / count(people);
|
|
@@ -421,4 +386,4 @@ people filter (p -> p.age > 30);
|
|
|
421
386
|
|
|
422
387
|
## License
|
|
423
388
|
|
|
424
|
-
*[Add license information here]*
|
|
389
|
+
*[Add license information here]*
|