@mojir/lits 2.1.34 → 2.1.35

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 CHANGED
@@ -78,31 +78,31 @@ let sum = +([1, 2, 3, 4, 5]);
78
78
 
79
79
  ```lits
80
80
  // Numbers
81
- 42 // integer
82
- 3.14 // float
83
- 0xFFFF // hexadecimal
84
- 0b1100 // binary
85
- 0o77 // octal
86
- -2.3e-2 // scientific notation
81
+ 42; // integer
82
+ 3.14; // float
83
+ 0xFFFF; // hexadecimal
84
+ 0b1100; // binary
85
+ 0o77; // octal
86
+ -2.3e-2; // scientific notation
87
87
 
88
88
  // Strings
89
- "Hello, world!"
89
+ "Hello, world!";
90
90
 
91
91
  // Booleans
92
- true
93
- false
92
+ true;
93
+ false;
94
94
 
95
95
  // Null
96
- null
96
+ null;
97
97
 
98
98
  // Arrays
99
- [1, 2, 3, 4]
99
+ [1, 2, 3, 4];
100
100
 
101
101
  // Objects
102
- { name: "John", age: 30 }
102
+ { name: "John", age: 30 };
103
103
 
104
104
  // Regular expressions
105
- #"pattern"
105
+ #"pattern";
106
106
  ```
107
107
 
108
108
  ### Mathematical Constants
@@ -110,26 +110,23 @@ null
110
110
  Lits provides predefined mathematical constants:
111
111
 
112
112
  ```lits
113
- PI // => 3.141592653589793
114
- π // => 3.141592653589793 (Unicode alternative)
115
- E // => 2.718281828459045 (Euler's number)
116
- ε // => 2.718281828459045 (Unicode alternative)
117
- PHI // => 1.618033988749895 (Golden ratio)
118
- φ // => 1.618033988749895 (Unicode alternative)
113
+ PI; // => 3.141592653589793
114
+ π; // => 3.141592653589793 (Unicode alternative)
115
+ E; // => 2.718281828459045 (Euler's number)
116
+ ε; // => 2.718281828459045 (Unicode alternative)
117
+ PHI; // => 1.618033988749895 (Golden ratio)
118
+ φ; // => 1.618033988749895 (Unicode alternative)
119
119
 
120
120
  // Infinity values
121
- POSITIVE_INFINITY // => Infinity
122
- // => Infinity (Unicode alternative)
123
- NEGATIVE_INFINITY // => -Infinity
121
+ POSITIVE_INFINITY; // => Infinity
122
+ ∞; // => Infinity (Unicode alternative)
123
+ NEGATIVE_INFINITY; // => -Infinity
124
124
 
125
125
  // Integer and float limits
126
- MAX_SAFE_INTEGER // => 9007199254740991
127
- MIN_SAFE_INTEGER // => -9007199254740991
128
- MAX_VALUE // => 1.7976931348623157e+308
129
- MIN_VALUE // => 5e-324
130
-
131
- // Not a Number
132
- NaN // => NaN
126
+ MAX_SAFE_INTEGER; // => 9007199254740991
127
+ MIN_SAFE_INTEGER; // => -9007199254740991
128
+ MAX_VALUE; // => 1.7976931348623157e+308
129
+ MIN_VALUE; // => 5e-324
133
130
  ```
134
131
 
135
132
  ## Special Expressions
@@ -144,7 +141,7 @@ let x = 10;
144
141
  // => 10
145
142
 
146
143
  // Variables are immutable
147
- let x = 20; // Error: x is already defined
144
+ // let x = 20; // Error: x is already defined
148
145
 
149
146
  // Shadowing in inner scopes
150
147
  let y = {
@@ -160,19 +157,27 @@ let y = {
160
157
  // Object destructuring
161
158
  let { name, age } = { name: "John", age: 30 };
162
159
  // name => "John", age => 30
160
+ ```
163
161
 
162
+ ```lits
164
163
  // Array destructuring
165
- let [first, second] = [1, 2, 3, 4];
166
- // first => 1, second => 2
164
+ let [a, b] = [1, 2, 3, 4];
165
+ // a => 1, b => 2
166
+ ```
167
167
 
168
+ ```lits
168
169
  // With default values
169
170
  let { name = "Unknown", age = 0 } = { name: "John" };
170
171
  // name => "John", age => 0
172
+ ```
171
173
 
174
+ ```lits
172
175
  // Rest patterns
173
176
  let [head, ...tail] = [1, 2, 3, 4];
174
177
  // head => 1, tail => [2, 3, 4]
178
+ ```
175
179
 
180
+ ```lits
176
181
  // Destructuring in function parameters
177
182
  let displayPerson = ({name, age}) ->
178
183
  name ++ " is " ++ str(age) ++ " years old";
@@ -196,10 +201,10 @@ let square = x -> x * x;
196
201
  let constant = () -> 42;
197
202
 
198
203
  // Positional arguments
199
- let add = -> $1 + $2;
204
+ let add-v2 = -> $1 + $2;
200
205
 
201
206
  // Single positional argument
202
- let square = -> $ * $;
207
+ let square-v2 = -> $ * $;
203
208
 
204
209
  // Self-reference for recursion
205
210
  let factorial = n ->
@@ -216,15 +221,17 @@ let factorial = n ->
216
221
 
217
222
  ```lits
218
223
  // If expression
224
+ let x = !:random-int(0, 20); // Random number between 0 and 19
225
+
219
226
  if x > 10 then
220
227
  "large"
221
228
  else
222
229
  "small"
223
- end
230
+ end;
224
231
  // => "large" (if x > 10) or "small" (if x <= 10)
225
232
 
226
233
  // If without else returns null
227
- if false then "never" end
234
+ if false then "never" end;
228
235
  // => null
229
236
 
230
237
  // Unless (inverted if)
@@ -232,13 +239,15 @@ unless x > 10 then
232
239
  "small"
233
240
  else
234
241
  "large"
235
- end
242
+ end;
236
243
  // => "small" (if x <= 10) or "large" (if x > 10)
237
244
  ```
238
245
 
239
246
  #### Cond
240
247
 
241
248
  ```lits
249
+ let x = !:random-int(0, 20); // Random number between 0 and 19
250
+
242
251
  // Multi-branch conditional
243
252
  cond
244
253
  case x < 5 then "small"
@@ -251,6 +260,8 @@ end ?? "extra large"
251
260
  #### Switch
252
261
 
253
262
  ```lits
263
+ let x = !:random-int(0, 3); // Random number between 0 and 2
264
+
254
265
  // Switch on value
255
266
  switch x
256
267
  case 0 then "zero"
@@ -266,27 +277,27 @@ end
266
277
 
267
278
  ```lits
268
279
  // Simple iteration
269
- for (x in [1, 2, 3, 4]) -> x * 2
280
+ for (x in [1, 2, 3, 4]) -> x * 2;
270
281
  // => [2, 4, 6, 8]
271
282
 
272
283
  // With filtering
273
- for (x in [1, 2, 3, 4] when odd?(x)) -> x * 2
284
+ for (x in [1, 2, 3, 4] when odd?(x)) -> x * 2;
274
285
  // => [2, 6]
275
286
 
276
287
  // With while condition
277
- for (x in [1, 2, 3, 4] while x < 3) -> x * 2
288
+ for (x in [1, 2, 3, 4] while x < 3) -> x * 2;
278
289
  // => [2, 4]
279
290
 
280
291
  // With let bindings
281
- for (x in [1, 2, 3] let doubled = x * 2) -> doubled + 1
292
+ for (x in [1, 2, 3] let doubled = x * 2) -> doubled + 1;
282
293
  // => [3, 5, 7]
283
294
 
284
295
  // Multiple iterators
285
- for (x in [1, 2], y in [10, 20]) -> x + y
296
+ for (x in [1, 2], y in [10, 20]) -> x + y;
286
297
  // => [11, 21, 12, 22]
287
298
 
288
299
  // Object iteration
289
- for ([key, value] in { a: 1, b: 2 }) -> key ++ ":" ++ str(value)
300
+ for (entry in { a: 1, b: 2 } let [key, value] = entry) -> key ++ ":" ++ str(value);
290
301
  // => ["a:1", "b:2"]
291
302
  ```
292
303
 
@@ -322,21 +333,25 @@ try
322
333
  riskyOperation()
323
334
  catch
324
335
  "Something went wrong"
325
- end
336
+ end;
326
337
 
327
338
  // With error binding
328
339
  try
329
340
  riskyOperation()
330
341
  catch (error)
331
342
  "Error: " ++ error.message
332
- end
343
+ end;
333
344
  ```
334
345
 
335
346
  #### Throw
336
347
 
337
348
  ```lits
338
349
  // Throwing errors
339
- throw("Custom error message")
350
+ try
351
+ throw("Custom error message");
352
+ catch
353
+ "Caught an error"
354
+ end;
340
355
 
341
356
  // In context
342
357
  let divide = (a, b) ->
@@ -353,27 +368,27 @@ let divide = (a, b) ->
353
368
 
354
369
  ```lits
355
370
  // Logical AND (short-circuit)
356
- true && "second value" // => "second value"
357
- false && "never reached" // => false
371
+ true && "second value"; // => "second value"
372
+ false && "never reached"; // => false
358
373
 
359
374
  // Logical OR (short-circuit)
360
- false || "default value" // => "default value"
361
- true || "never reached" // => true
375
+ false || "default value"; // => "default value"
376
+ true || "never reached"; // => true
362
377
 
363
378
  // Multiple arguments
364
- &&(true, true, "all true") // => "all true"
365
- ||(false, false, "found") // => "found"
379
+ &&(true, true, "all true"); // => "all true"
380
+ ||(false, false, "found"); // => "found"
366
381
  ```
367
382
 
368
383
  #### Null Coalescing
369
384
 
370
385
  ```lits
371
386
  // Null coalescing operator
372
- null ?? "default" // => "default"
373
- undefined ?? "default" // => "default"
374
- 0 ?? "default" // => 0 (only null/undefined are coalesced)
375
- false ?? "default" // => false
376
- "" ?? "default" // => ""
387
+ null ?? "default"; // => "default"
388
+ undefined ?? "default"; // => "default"
389
+ 0 ?? "default"; // => 0 (only null/undefined are coalesced)
390
+ false ?? "default"; // => false
391
+ "" ?? "default"; // => ""
377
392
  ```
378
393
 
379
394
  ### Blocks
@@ -394,24 +409,25 @@ false ?? "default" // => false
394
409
 
395
410
  ```lits
396
411
  // Array literal
397
- [1, 2, 3, 4]
412
+ [1, 2, 3, 4];
398
413
 
399
414
  // Array function
400
- array(1, 2, 3, 4)
415
+ array(1, 2, 3, 4);
401
416
 
402
417
  // With spread
403
- [1, 2, ...[3, 4, 5], 6]
404
- // => [1, 2, 3, 4, 5, 6]
418
+ let small-set = [3, 4, 5];
419
+ [1, 2, ...small-set, 6];
420
+ // => [1, 2, 3, 4, 5, 6];
405
421
  ```
406
422
 
407
423
  #### Object Construction
408
424
 
409
425
  ```lits
410
426
  // Object literal
411
- { name: "John", age: 30 }
427
+ { name: "John", age: 30 };
412
428
 
413
429
  // Object function
414
- object("name", "John", "age", 30)
430
+ object("name", "John", "age", 30);
415
431
 
416
432
  // With spread
417
433
  let defaults = { type: "Person", active: true };
@@ -419,7 +435,7 @@ let defaults = { type: "Person", active: true };
419
435
  ...defaults,
420
436
  name: "John",
421
437
  age: 30
422
- }
438
+ };
423
439
  // => { type: "Person", active: true, name: "John", age: 30 }
424
440
  ```
425
441
 
@@ -448,35 +464,35 @@ All functions that take two parameters can be used as operators:
448
464
 
449
465
  ```lits
450
466
  // As a function
451
- max(5, 10) // => 10
467
+ max(5, 10); // => 10
452
468
 
453
469
  // As an operator
454
- 5 max 10 // => 10
470
+ 5 max 10; // => 10
455
471
  ```
456
472
 
457
473
  All operators can be used as functions:
458
474
 
459
475
  ```lits
460
476
  // As an operator
461
- 5 + 3 // => 8
477
+ 5 + 3; // => 8
462
478
 
463
479
  // As a function
464
- +(5, 3) // => 8
480
+ +(5, 3); // => 8
465
481
 
466
482
  // Partial application with underscore placeholder
467
483
  let add5 = +(5, _);
468
- add5(3) // => 8
484
+ add5(3); // => 8
469
485
 
470
486
  // Multiple placeholders
471
- let subtractAndDivide = /(-(_, _), 2);
472
- subtractAndDivide(10, 3) // => 3.5 (10 - 3 = 7, then 7 / 2 = 3.5)
487
+ let subtractTwoValues = -(100, _, _);
488
+ subtractTwoValues(4, 3); // => 93
473
489
 
474
490
  // Single placeholder in different positions
475
491
  let subtract = -(_, 2);
476
- subtract(10) // => 8
492
+ subtract(10); // => 8
477
493
 
478
494
  let divide = /(10, _);
479
- divide(2) // => 5
495
+ divide(2); // => 5
480
496
  ```
481
497
 
482
498
  ### Parameter Order
@@ -485,17 +501,10 @@ Lits favors subject-first parameter order for better operator chaining:
485
501
 
486
502
  ```lits
487
503
  // Function style
488
- filter([1, 2, 3, 4], odd?) // => [1, 3]
504
+ filter([1, 2, 3, 4], odd?); // => [1, 3]
489
505
 
490
506
  // Operator style (more readable)
491
- [1, 2, 3, 4] filter odd? // => [1, 3]
492
-
493
- // Chaining
494
- [1, 2, 3, 4, 5, 6]
495
- filter odd?
496
- map square
497
- reduce +
498
- // => 35
507
+ [1, 2, 3, 4] filter odd?; // => [1, 3]
499
508
  ```
500
509
 
501
510
  ### Pipe Operator
@@ -504,13 +513,13 @@ The pipe operator `|>` passes the result of the left expression as the first arg
504
513
 
505
514
  ```lits
506
515
  // Without pipe operator
507
- reduce(map(filter([1, 2, 3, 4, 5, 6], odd?), square), +)
516
+ reduce(map(filter([1, 2, 3, 4, 5, 6], odd?), -> $ + $), +, 0);
508
517
 
509
518
  // With pipe operator (much more readable)
510
519
  [1, 2, 3, 4, 5, 6]
511
520
  |> filter(_, odd?)
512
- |> map(_, square)
513
- |> reduce(_, +)
521
+ |> map(_, -> $ * $)
522
+ |> reduce(_, +, 0);
514
523
  // => 35
515
524
 
516
525
  // Simple transformations
@@ -518,14 +527,14 @@ reduce(map(filter([1, 2, 3, 4, 5, 6], odd?), square), +)
518
527
  |> upper-case
519
528
  |> split(_, " ")
520
529
  |> reverse
521
- |> join(_, "-")
530
+ |> join(_, "-");
522
531
  // => "WORLD-HELLO"
523
532
 
524
533
  // Mathematical operations
525
534
  10
526
535
  |> +(_, 5)
527
536
  |> *(_, 2)
528
- |> /(_, 3)
537
+ |> /(_, 3);
529
538
  // => 10 (10 + 5 = 15, 15 * 2 = 30, 30 / 3 = 10)
530
539
 
531
540
  // Data processing pipeline
@@ -533,7 +542,7 @@ reduce(map(filter([1, 2, 3, 4, 5, 6], odd?), square), +)
533
542
  |> get(_, "numbers")
534
543
  |> filter(_, even?)
535
544
  |> map(_, *(_, 3))
536
- |> reduce(_, +)
545
+ |> reduce(_, +, 0);
537
546
  // => 18 (even numbers [2, 4] -> [6, 12] -> sum = 18)
538
547
  ```
539
548
 
@@ -557,7 +566,7 @@ For a complete reference of all available functions with examples, visit the [Li
557
566
 
558
567
  ```lits
559
568
  // Export variables and functions
560
- export let PI = 3.14159;
569
+ export let pi = 3.14159;
561
570
  export let square = x -> x * x;
562
571
 
563
572
  // Exported values become available to other modules
@@ -585,14 +594,14 @@ let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
585
594
 
586
595
  // Get even numbers squared
587
596
  let evenSquares = numbers
588
- filter even?
589
- map square;
597
+ |> filter(_, even?)
598
+ |> map(_, -> $ * $);
590
599
  // => [4, 16, 36, 64, 100]
591
600
 
592
601
  // Sum of odd numbers
593
602
  let oddSum = numbers
594
- filter odd?
595
- reduce +;
603
+ |> filter(_, odd?)
604
+ |> reduce(_, +, 0);
596
605
  // => 25
597
606
  ```
598
607
 
@@ -626,8 +635,8 @@ let gradeToLetter = grade ->
626
635
  case 60 then "D"
627
636
  end ?? "F";
628
637
 
629
- gradeToLetter(80) // => "B"
630
- gradeToLetter(55) // => "F"
638
+ gradeToLetter(80); // => "B"
639
+ gradeToLetter(55); // => "F"
631
640
 
632
641
  // HTTP status code handling
633
642
  let statusMessage = code ->
@@ -649,8 +658,8 @@ let describe = x ->
649
658
  case array?(x) then "list of " ++ str(count(x)) ++ " items"
650
659
  end ?? "unknown type";
651
660
 
652
- describe(42) // => "a number: 42"
653
- describe([1,2,3]) // => "list of 3 items"
661
+ describe(42); // => "a number: 42"
662
+ describe([1,2,3]); // => "list of 3 items"
654
663
 
655
664
  // Grade ranges
656
665
  let gradeToLetter = score ->
@@ -661,8 +670,8 @@ let gradeToLetter = score ->
661
670
  case score >= 60 then "D"
662
671
  end ?? "F";
663
672
 
664
- gradeToLetter(85) // => "B"
665
- gradeToLetter(55) // => "F"
673
+ gradeToLetter(85); // => "B"
674
+ gradeToLetter(55); // => "F"
666
675
  ```
667
676
 
668
677
  ## JavaScript Interoperability