aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/doc/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/doc/README.md')
-rwxr-xr-xdoc/docs/doc/README.md180
1 files changed, 101 insertions, 79 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index c60330a..6662752 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -389,6 +389,80 @@ tb::func! if tb != nil
389</pre> 389</pre>
390</YueDisplay> 390</YueDisplay>
391 391
392### Chaining Comparisons
393
394Comparisons can be arbitrarily chained:
395
396```moonscript
397print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
398-- output: true
399
400a = 5
401print 1 <= a <= 10
402-- output: true
403```
404<YueDisplay>
405<pre>
406print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
407-- output: true
408
409a = 5
410print 1 <= a <= 10
411-- output: true
412</pre>
413</YueDisplay>
414
415Note the evaluation behavior of chained comparisons:
416
417```moonscript
418v = (x)->
419 print x
420 x
421
422print v(1) < v(2) <= v(3)
423--[[
424 output:
425 2
426 1
427 3
428 true
429]]
430
431print v(1) > v(2) <= v(3)
432--[[
433 output:
434 2
435 1
436 false
437]]
438```
439<YueDisplay>
440<pre>
441v = (x)->
442 print x
443 x
444
445print v(1) < v(2) <= v(3)
446--[[
447 output:
448 2
449 1
450 3
451true
452]]
453
454print v(1) > v(2) <= v(3)
455--[[
456 output:
457 2
458 1
459 false
460]]
461</pre>
462</YueDisplay>
463
464The middle expression is only evaluated once, rather than twice as it would be if the expression were written as `v(1) < v(2) and v(2) <= v(3)`. However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit `and` operator should be used explicitly.
465
392### Table Appending 466### Table Appending
393The **[] =** operator is used to append values to tables. 467The **[] =** operator is used to append values to tables.
394 468
@@ -984,17 +1058,17 @@ Typically when you see a table literal, {1,2,3}, it is on the right hand side of
984This is best explained with examples. Here is how you would unpack the first two values from a table: 1058This is best explained with examples. Here is how you would unpack the first two values from a table:
985 1059
986```moonscript 1060```moonscript
987thing = {1, 2} 1061thing = [1, 2]
988 1062
989{a, b} = thing 1063[a, b] = thing
990print a, b 1064print a, b
991``` 1065```
992<YueDisplay> 1066<YueDisplay>
993 1067
994<pre> 1068<pre>
995thing = {1, 2} 1069thing = [1, 2]
996 1070
997{a, b} = thing 1071[a, b] = thing
998print a, b 1072print a, b
999</pre> 1073</pre>
1000</YueDisplay> 1074</YueDisplay>
@@ -1921,6 +1995,19 @@ t = {
1921</pre> 1995</pre>
1922</YueDisplay> 1996</YueDisplay>
1923 1997
1998Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed.
1999
2000```moonscript
2001some_values = [ 1, 2, 3, 4 ]
2002list_with_one_element = [ 1, ]
2003```
2004<YueDisplay>
2005<pre>
2006some_values = [ 1, 2, 3, 4 ]
2007list_with_one_element = [ 1, ]
2008</pre>
2009</YueDisplay>
2010
1924## Comprehensions 2011## Comprehensions
1925 2012
1926Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. 2013Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration.
@@ -1930,12 +2017,12 @@ Comprehensions provide a convenient syntax for constructing a new table by itera
1930The following creates a copy of the items table but with all the values doubled. 2017The following creates a copy of the items table but with all the values doubled.
1931 2018
1932```moonscript 2019```moonscript
1933items = { 1, 2, 3, 4 } 2020items = [ 1, 2, 3, 4 ]
1934doubled = [item * 2 for i, item in ipairs items] 2021doubled = [item * 2 for i, item in ipairs items]
1935``` 2022```
1936<YueDisplay> 2023<YueDisplay>
1937<pre> 2024<pre>
1938items = { 1, 2, 3, 4 } 2025items = [ 1, 2, 3, 4 ]
1939doubled = [item * 2 for i, item in ipairs items] 2026doubled = [item * 2 for i, item in ipairs items]
1940</pre> 2027</pre>
1941</YueDisplay> 2028</YueDisplay>
@@ -1969,18 +2056,18 @@ The for and when clauses can be chained as much as desired. The only requirement
1969Using multiple for clauses is the same as using nested loops: 2056Using multiple for clauses is the same as using nested loops:
1970 2057
1971```moonscript 2058```moonscript
1972x_coords = {4, 5, 6, 7} 2059x_coords = [4, 5, 6, 7]
1973y_coords = {9, 2, 3} 2060y_coords = [9, 2, 3]
1974 2061
1975points = [{x, y} for x in *x_coords \ 2062points = [ [x, y] for x in *x_coords \
1976for y in *y_coords] 2063for y in *y_coords]
1977``` 2064```
1978<YueDisplay> 2065<YueDisplay>
1979<pre> 2066<pre>
1980x_coords = {4, 5, 6, 7} 2067x_coords = [4, 5, 6, 7]
1981y_coords = {9, 2, 3} 2068y_coords = [9, 2, 3]
1982 2069
1983points = [{x, y} for x in *x_coords \ 2070points = [ [x, y] for x in *x_coords \
1984for y in *y_coords] 2071for y in *y_coords]
1985</pre> 2072</pre>
1986</YueDisplay> 2073</YueDisplay>
@@ -2035,12 +2122,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"}
2035The **\*** operator is also supported. Here we create a square root look up table for a few numbers. 2122The **\*** operator is also supported. Here we create a square root look up table for a few numbers.
2036 2123
2037```moonscript 2124```moonscript
2038numbers = {1, 2, 3, 4} 2125numbers = [1, 2, 3, 4]
2039sqrts = {i, math.sqrt i for i in *numbers} 2126sqrts = {i, math.sqrt i for i in *numbers}
2040``` 2127```
2041<YueDisplay> 2128<YueDisplay>
2042<pre> 2129<pre>
2043numbers = {1, 2, 3, 4} 2130numbers = [1, 2, 3, 4]
2044sqrts = {i, math.sqrt i for i in *numbers} 2131sqrts = {i, math.sqrt i for i in *numbers}
2045</pre> 2132</pre>
2046</YueDisplay> 2133</YueDisplay>
@@ -2405,15 +2492,6 @@ You can write range checking code with an `in-expression`.
2405```moonscript 2492```moonscript
2406a = 5 2493a = 5
2407 2494
2408if a in [1, 10]
2409 print "a is in range from 1 to 10"
2410
2411if a not in [1, 10]
2412 print "a is not in range from 1 to 10"
2413
2414if a in (0, 11)
2415 print "a is between 0 and 11 with open intervals"
2416
2417if a in {1, 3, 5, 7} 2495if a in {1, 3, 5, 7}
2418 print "checking equality with discrete values" 2496 print "checking equality with discrete values"
2419 2497
@@ -2424,15 +2502,6 @@ if a in list
2424<pre> 2502<pre>
2425a = 5 2503a = 5
2426 2504
2427if a in [1, 10]
2428 print "a is in range from 1 to 10"
2429
2430if a not in [1, 10]
2431 print "a is not in range from 1 to 10"
2432
2433if a in (0, 11)
2434 print "a is between 0 and 11 with open intervals"
2435
2436if a in {1, 3, 5, 7} 2505if a in {1, 3, 5, 7}
2437 print "checking equality with discrete values" 2506 print "checking equality with discrete values"
2438 2507
@@ -2649,53 +2718,6 @@ switch item
2649</pre> 2718</pre>
2650</YueDisplay> 2719</YueDisplay>
2651 2720
2652### Range Matching
2653
2654You can do range matching in a switch when clause using `In` expressions.
2655
2656```moonscript
2657value = 5
2658
2659switch item
2660 -- range checking with closed interval
2661 when in [1, 3]
2662 print "1 <= value <= 3"
2663
2664 -- range checking with open and closed interval
2665 when in (6, 8]
2666 print "6 < value <= 8"
2667
2668 -- not in range checking
2669 when not in [1, 10)
2670 print "not (1 <= value < 10)"
2671
2672 -- checking discrete values
2673 when in {11, 21, 99}
2674 print "value is 11, 21 or 99"
2675```
2676<YueDisplay>
2677<pre>
2678value = 5
2679
2680switch item
2681 -- range checking with closed interval
2682 when in [1, 3]
2683 print "1 <= value <= 3"
2684
2685 -- range checking with open and closed interval
2686 when in (6, 8]
2687 print "6 < value <= 8"
2688
2689 -- not in range checking
2690 when not in [1, 10)
2691 print "not (1 <= value < 10)"
2692
2693 -- checking discrete values
2694 when in {11, 21, 99}
2695 print "value is 11, 21 or 99"
2696</pre>
2697</YueDisplay>
2698
2699## Object Oriented Programming 2721## Object Oriented Programming
2700 2722
2701In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details. 2723In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details.