diff options
Diffstat (limited to 'doc/docs/doc/README.md')
-rwxr-xr-x | doc/docs/doc/README.md | 180 |
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 | |||
394 | Comparisons can be arbitrarily chained: | ||
395 | |||
396 | ```moonscript | ||
397 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
398 | -- output: true | ||
399 | |||
400 | a = 5 | ||
401 | print 1 <= a <= 10 | ||
402 | -- output: true | ||
403 | ``` | ||
404 | <YueDisplay> | ||
405 | <pre> | ||
406 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
407 | -- output: true | ||
408 | |||
409 | a = 5 | ||
410 | print 1 <= a <= 10 | ||
411 | -- output: true | ||
412 | </pre> | ||
413 | </YueDisplay> | ||
414 | |||
415 | Note the evaluation behavior of chained comparisons: | ||
416 | |||
417 | ```moonscript | ||
418 | v = (x)-> | ||
419 | print x | ||
420 | x | ||
421 | |||
422 | print v(1) < v(2) <= v(3) | ||
423 | --[[ | ||
424 | output: | ||
425 | 2 | ||
426 | 1 | ||
427 | 3 | ||
428 | true | ||
429 | ]] | ||
430 | |||
431 | print v(1) > v(2) <= v(3) | ||
432 | --[[ | ||
433 | output: | ||
434 | 2 | ||
435 | 1 | ||
436 | false | ||
437 | ]] | ||
438 | ``` | ||
439 | <YueDisplay> | ||
440 | <pre> | ||
441 | v = (x)-> | ||
442 | print x | ||
443 | x | ||
444 | |||
445 | print v(1) < v(2) <= v(3) | ||
446 | --[[ | ||
447 | output: | ||
448 | 2 | ||
449 | 1 | ||
450 | 3 | ||
451 | true | ||
452 | ]] | ||
453 | |||
454 | print v(1) > v(2) <= v(3) | ||
455 | --[[ | ||
456 | output: | ||
457 | 2 | ||
458 | 1 | ||
459 | false | ||
460 | ]] | ||
461 | </pre> | ||
462 | </YueDisplay> | ||
463 | |||
464 | The 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 |
393 | The **[] =** operator is used to append values to tables. | 467 | The **[] =** 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 | |||
984 | This is best explained with examples. Here is how you would unpack the first two values from a table: | 1058 | This is best explained with examples. Here is how you would unpack the first two values from a table: |
985 | 1059 | ||
986 | ```moonscript | 1060 | ```moonscript |
987 | thing = {1, 2} | 1061 | thing = [1, 2] |
988 | 1062 | ||
989 | {a, b} = thing | 1063 | [a, b] = thing |
990 | print a, b | 1064 | print a, b |
991 | ``` | 1065 | ``` |
992 | <YueDisplay> | 1066 | <YueDisplay> |
993 | 1067 | ||
994 | <pre> | 1068 | <pre> |
995 | thing = {1, 2} | 1069 | thing = [1, 2] |
996 | 1070 | ||
997 | {a, b} = thing | 1071 | [a, b] = thing |
998 | print a, b | 1072 | print 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 | ||
1998 | Lua 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 | ||
2001 | some_values = [ 1, 2, 3, 4 ] | ||
2002 | list_with_one_element = [ 1, ] | ||
2003 | ``` | ||
2004 | <YueDisplay> | ||
2005 | <pre> | ||
2006 | some_values = [ 1, 2, 3, 4 ] | ||
2007 | list_with_one_element = [ 1, ] | ||
2008 | </pre> | ||
2009 | </YueDisplay> | ||
2010 | |||
1924 | ## Comprehensions | 2011 | ## Comprehensions |
1925 | 2012 | ||
1926 | Comprehensions 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. | 2013 | Comprehensions 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 | |||
1930 | The following creates a copy of the items table but with all the values doubled. | 2017 | The following creates a copy of the items table but with all the values doubled. |
1931 | 2018 | ||
1932 | ```moonscript | 2019 | ```moonscript |
1933 | items = { 1, 2, 3, 4 } | 2020 | items = [ 1, 2, 3, 4 ] |
1934 | doubled = [item * 2 for i, item in ipairs items] | 2021 | doubled = [item * 2 for i, item in ipairs items] |
1935 | ``` | 2022 | ``` |
1936 | <YueDisplay> | 2023 | <YueDisplay> |
1937 | <pre> | 2024 | <pre> |
1938 | items = { 1, 2, 3, 4 } | 2025 | items = [ 1, 2, 3, 4 ] |
1939 | doubled = [item * 2 for i, item in ipairs items] | 2026 | doubled = [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 | |||
1969 | Using multiple for clauses is the same as using nested loops: | 2056 | Using multiple for clauses is the same as using nested loops: |
1970 | 2057 | ||
1971 | ```moonscript | 2058 | ```moonscript |
1972 | x_coords = {4, 5, 6, 7} | 2059 | x_coords = [4, 5, 6, 7] |
1973 | y_coords = {9, 2, 3} | 2060 | y_coords = [9, 2, 3] |
1974 | 2061 | ||
1975 | points = [{x, y} for x in *x_coords \ | 2062 | points = [ [x, y] for x in *x_coords \ |
1976 | for y in *y_coords] | 2063 | for y in *y_coords] |
1977 | ``` | 2064 | ``` |
1978 | <YueDisplay> | 2065 | <YueDisplay> |
1979 | <pre> | 2066 | <pre> |
1980 | x_coords = {4, 5, 6, 7} | 2067 | x_coords = [4, 5, 6, 7] |
1981 | y_coords = {9, 2, 3} | 2068 | y_coords = [9, 2, 3] |
1982 | 2069 | ||
1983 | points = [{x, y} for x in *x_coords \ | 2070 | points = [ [x, y] for x in *x_coords \ |
1984 | for y in *y_coords] | 2071 | for 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"} | |||
2035 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. | 2122 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. |
2036 | 2123 | ||
2037 | ```moonscript | 2124 | ```moonscript |
2038 | numbers = {1, 2, 3, 4} | 2125 | numbers = [1, 2, 3, 4] |
2039 | sqrts = {i, math.sqrt i for i in *numbers} | 2126 | sqrts = {i, math.sqrt i for i in *numbers} |
2040 | ``` | 2127 | ``` |
2041 | <YueDisplay> | 2128 | <YueDisplay> |
2042 | <pre> | 2129 | <pre> |
2043 | numbers = {1, 2, 3, 4} | 2130 | numbers = [1, 2, 3, 4] |
2044 | sqrts = {i, math.sqrt i for i in *numbers} | 2131 | sqrts = {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 |
2406 | a = 5 | 2493 | a = 5 |
2407 | 2494 | ||
2408 | if a in [1, 10] | ||
2409 | print "a is in range from 1 to 10" | ||
2410 | |||
2411 | if a not in [1, 10] | ||
2412 | print "a is not in range from 1 to 10" | ||
2413 | |||
2414 | if a in (0, 11) | ||
2415 | print "a is between 0 and 11 with open intervals" | ||
2416 | |||
2417 | if a in {1, 3, 5, 7} | 2495 | if 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> |
2425 | a = 5 | 2503 | a = 5 |
2426 | 2504 | ||
2427 | if a in [1, 10] | ||
2428 | print "a is in range from 1 to 10" | ||
2429 | |||
2430 | if a not in [1, 10] | ||
2431 | print "a is not in range from 1 to 10" | ||
2432 | |||
2433 | if a in (0, 11) | ||
2434 | print "a is between 0 and 11 with open intervals" | ||
2435 | |||
2436 | if a in {1, 3, 5, 7} | 2505 | if 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 | |||
2654 | You can do range matching in a switch when clause using `In` expressions. | ||
2655 | |||
2656 | ```moonscript | ||
2657 | value = 5 | ||
2658 | |||
2659 | switch 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> | ||
2678 | value = 5 | ||
2679 | |||
2680 | switch 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 | ||
2701 | In 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. | 2723 | In 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. |