diff options
author | Li Jin <dragon-fly@qq.com> | 2022-08-16 11:14:14 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-08-16 11:14:14 +0800 |
commit | 6eda1c0cb67dca6ada35617faa5abda2146fb209 (patch) | |
tree | caa27bd7f25eddae05984b626926abeba7853041 | |
parent | 82b86397797ecd4d1782ee75abf7933e5d973e3c (diff) | |
download | yuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.tar.gz yuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.tar.bz2 yuescript-6eda1c0cb67dca6ada35617faa5abda2146fb209.zip |
update doc.
-rwxr-xr-x | doc/docs/doc/README.md | 64 |
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 | ||
544 | Instead of a series of nested function calls, you can pipe values with operator **|>**. | 544 | Instead of a series of nested function calls, you can pipe values with operator **|>**. |
545 | |||
545 | ```moonscript | 546 | ```moonscript |
546 | "hello" |> print | 547 | "hello" |> print |
547 | 1 |> print 2 -- insert pipe item as the first argument | 548 | 1 |> print 2 -- insert pipe item as the first argument |
@@ -560,7 +561,6 @@ readFile "example.txt" | |||
560 | "hello" |> print | 561 | "hello" |> print |
561 | 1 |> print 2 -- insert pipe item as the first argument | 562 | 1 |> print 2 -- insert pipe item as the first argument |
562 | 2 |> print 1, _, 3 -- pipe with a placeholder | 563 | 2 |> print 1, _, 3 -- pipe with a placeholder |
563 | |||
564 | -- pipe expression in multiline | 564 | -- pipe expression in multiline |
565 | readFile "example.txt" | 565 | readFile "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 | |||
576 | Backcalls 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 | ||
580 | print "hello" | ||
581 | ``` | ||
582 | <YueDisplay> | ||
583 | <pre> | ||
584 | <- f | ||
585 | print "hello" | ||
586 | </pre> | ||
587 | </YueDisplay> | ||
588 | |||
589 | Fat arrow functions are also available. | ||
590 | |||
591 | ```moonscript | ||
592 | <= f | ||
593 | print @value | ||
594 | ``` | ||
595 | <YueDisplay> | ||
596 | <pre> | ||
597 | <= f | ||
598 | print @value | ||
599 | </pre> | ||
600 | </YueDisplay> | ||
601 | |||
602 | You 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} | ||
606 | x * 2 | ||
607 | ``` | ||
608 | <YueDisplay> | ||
609 | <pre> | ||
610 | (x) <- map _, {1, 2, 3} | ||
611 | x * 2 | ||
612 | </pre> | ||
613 | </YueDisplay> | ||
614 | |||
615 | If you wish to have further code after your backcalls, you can set them aside with a do statement. | ||
616 | |||
617 | ```moonscript | ||
618 | result, msg = do | ||
619 | (data) <- readAsync "filename.txt" | ||
620 | print data | ||
621 | (info) <- processAsync data | ||
622 | check info | ||
623 | print result, msg | ||
624 | ``` | ||
625 | <YueDisplay> | ||
626 | <pre> | ||
627 | result, msg = do | ||
628 | (data) <- readAsync "filename.txt" | ||
629 | print data | ||
630 | (info) <- processAsync data | ||
631 | check info | ||
632 | print result, msg | ||
633 | </pre> | ||
634 | </YueDisplay> | ||
635 | |||
574 | ### Nil Coalescing | 636 | ### Nil Coalescing |
575 | 637 | ||
576 | The 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. | 638 | The 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. |