aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-08-16 11:14:14 +0800
committerLi Jin <dragon-fly@qq.com>2022-08-16 11:14:14 +0800
commit6eda1c0cb67dca6ada35617faa5abda2146fb209 (patch)
treecaa27bd7f25eddae05984b626926abeba7853041
parent82b86397797ecd4d1782ee75abf7933e5d973e3c (diff)
downloadyuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.tar.gz
yuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.tar.bz2
yuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.zip
update doc.
-rwxr-xr-xdoc/docs/doc/README.md64
1 files changed, 63 insertions, 1 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 90cb309..c6b493c 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -542,6 +542,7 @@ with? io.open "test.txt", "w"
542### Piping 542### Piping
543 543
544Instead of a series of nested function calls, you can pipe values with operator **|>**. 544Instead of a series of nested function calls, you can pipe values with operator **|>**.
545
545```moonscript 546```moonscript
546"hello" |> print 547"hello" |> print
5471 |> print 2 -- insert pipe item as the first argument 5481 |> print 2 -- insert pipe item as the first argument
@@ -560,7 +561,6 @@ readFile "example.txt"
560"hello" |> print 561"hello" |> print
5611 |> print 2 -- insert pipe item as the first argument 5621 |> print 2 -- insert pipe item as the first argument
5622 |> print 1, _, 3 -- pipe with a placeholder 5632 |> print 1, _, 3 -- pipe with a placeholder
563
564-- pipe expression in multiline 564-- pipe expression in multiline
565readFile "example.txt" 565readFile "example.txt"
566 |> extract language, {} 566 |> extract language, {}
@@ -571,6 +571,68 @@ readFile "example.txt"
571</pre> 571</pre>
572</YueDisplay> 572</YueDisplay>
573 573
574### Backcall
575
576Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent.
577
578```moonscript
579<- f
580print "hello"
581```
582<YueDisplay>
583<pre>
584<- f
585print "hello"
586</pre>
587</YueDisplay>
588
589Fat arrow functions are also available.
590
591```moonscript
592<= f
593print @value
594```
595<YueDisplay>
596<pre>
597<= f
598print @value
599</pre>
600</YueDisplay>
601
602You can specify a placeholder for where you want the backcall function to go as a parameter.
603
604```moonscript
605(x) <- map _, {1, 2, 3}
606x * 2
607```
608<YueDisplay>
609<pre>
610(x) <- map _, {1, 2, 3}
611x * 2
612</pre>
613</YueDisplay>
614
615If you wish to have further code after your backcalls, you can set them aside with a do statement.
616
617```moonscript
618result, msg = do
619 (data) <- readAsync "filename.txt"
620 print data
621 (info) <- processAsync data
622 check info
623print result, msg
624```
625<YueDisplay>
626<pre>
627result, msg = do
628 (data) <- readAsync "filename.txt"
629 print data
630 (info) <- processAsync data
631 check info
632print result, msg
633</pre>
634</YueDisplay>
635
574### Nil Coalescing 636### Nil Coalescing
575 637
576The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil. 638The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil.