-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor readability suggestions for the online MoonBit tour (#360)
* Minor readability suggestions Signed-off-by: Glenn Lewis <[email protected]> * minor change in chapter3, lesson3 The pattern can also be used in let statements and guard expressions --------- Signed-off-by: Glenn Lewis <[email protected]> Co-authored-by: Yorkin <[email protected]>
- Loading branch information
Showing
21 changed files
with
80 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# Variables | ||
|
||
The `let` keyword used to define a variable. | ||
The `let` keyword is used to define a variable. | ||
|
||
The type of the variable can be annotated by using a colon followed by the type. | ||
It is optional, if not provided the type will be inferred from the value. | ||
The type of the variable can be annotated by using a colon followed by the type. | ||
It is optional; if not provided, the type will be inferred from the value. | ||
|
||
Variables are immutable by default in MoonBit. You can add an extra `mut` | ||
Variables are immutable by default in MoonBit. You can add an extra `mut` | ||
keyword to make them mutable at the local level. | ||
|
||
If you uncomment the `d = d + 1`, you will get an error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
# String | ||
|
||
A string is a sequence of characters encoded in UTF-16. In MoonBit, strings are immutable, | ||
A string is a sequence of characters encoded in UTF-16. In MoonBit, strings are immutable, | ||
which means you cannot change the elements inside a string. | ||
|
||
MoonBit supports C-style escape characters in strings and chars, such as `\n`, `\t`, `\\`, `\"`, and `\'`. | ||
MoonBit supports C-style escape characters in strings and chars, such as `\n` (newline), | ||
`\t` (tab), `\\` (backslash), `\"` (double-quote), and `\'` (single-quote). | ||
|
||
Unicode escape characters are also supported. You can use `\u{}` to represent a Unicode character by its code point. | ||
Unicode escape characters are also supported. You can use `\u{...}` (where `...` represents | ||
the Unicode character's hex code) to represent a Unicode character by its code point. | ||
|
||
MoonBit also supports string interpolation written like `\{variable}`, which allows you to embed expressions into strings. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# Tuple | ||
|
||
Tuple is a collection of values that can have different types. It is immutable, | ||
A tuple is a collection of values that can have different types. It is immutable, | ||
which means that once it is created, it cannot be changed. It is created using | ||
parentheses. | ||
|
||
You can access the elements of tuple via the index: `tuple.0`, `tuple.1`, etc. | ||
|
||
Tuple can be destructed via syntax like `let (a,b) = tuple`, the `tuple` in | ||
right side is a tuple with two elements, and `a` and `b` are the variables to | ||
store the elements. This is a special use case of *pattern matching*. We will | ||
introduce *pattern matching* in the later chapter. | ||
A tuple can be destructed via syntax like `let (a,b) = tuple`, where the `tuple` on | ||
the right side is a tuple with two elements, and `a` and `b` are the variables to | ||
store the elements. This is a special use case of *pattern matching* which we will | ||
introduce in a later chapter. | ||
|
||
It's common to use tuple to return multiple values from a function. | ||
It's common to use a tuple to return multiple values from a function. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
moonbit-tour/tour/chapter2_data_types/lesson1_struct/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
moonbit-tour/tour/chapter3_pattern_matching/lesson1_introduction/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# Pattern Matching | ||
|
||
We have seen pattern matching in the previous example. | ||
It's a powerful feature in MoonBit that can be used in many places. It can help you test conditions conveniently and effectively, making your program more precise and robust. | ||
It's a powerful feature in MoonBit that can be used in many places. It can help you test conditions conveniently and effectively, making your programs more precise and robust. | ||
|
||
In this example, we give some basic use cases of pattern matching. Some other languages call it "destructuring" or "structured bindings", a way to extract values from a complex data structure. | ||
In this example, we give some basic use cases of pattern matching. Some other languages call it "destructuring" or "structured bindings", a way to extract values from a complex data structure. | ||
|
||
"Destructuring" is just a subset of this feature. | ||
In MoonBit, almost every type you can construct can have a form to "destruct", which we call a *pattern*. | ||
In MoonBit, almost every type you can construct can have a form to "destruct", which we call a *pattern*. |
22 changes: 13 additions & 9 deletions
22
moonbit-tour/tour/chapter3_pattern_matching/lesson2_let_and_match/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
# Pattern in let and match | ||
|
||
There are two common place to use pattern: let and match. | ||
There are two common places to use a pattern: `let` and `match`. | ||
|
||
We defined a `Resource` type, it describes a file system. The `Resource` can be a text file, an image, or a folder associated with more files. | ||
In this example, we define a `Resource` type that describes a file system. | ||
The `Resource` can be a text file, an image, or a folder associated with more files. | ||
|
||
## Pattern in let statement | ||
|
||
In a let statement, the left side of `=` can be a pattern, we known that assets is a folder so just use `let Folder(top_level) = assets` to match it and extract the inside map. | ||
In a `let` statement, the left side of `=` can be a pattern. | ||
We know that `assets` is a folder so we just use `let Folder(top_level) = assets` to match it and extract the value into the immutable variable `top_level`. | ||
|
||
You may notice that there is a partial match warning because the resource can also be `Image` or `TextFile`. **Partial match make the program more fragile: the pattern matching will fail in other cases and lead to the program aborting.** Practically, the match expression is used more frequently. | ||
You may notice that there is a partial match warning because the resource can also be `Image` or `TextFile`. | ||
**Partial matches make the program more fragile: the pattern matching may fail in other cases and lead to the program aborting.** | ||
Practically, the `match` expression is used more frequently than the `let` statement. | ||
|
||
## Pattern in match expression | ||
|
||
The `count` function traverse the input `res` recursively and return the count of `Image` and `TextFile`, using match expression. | ||
The `count` function traverses the input `res` recursively and returns the count of `Image` and `TextFile`, using a `match` expression. | ||
|
||
Match expressions have *first match semantics*. They will try to find the first matching pattern from the first case to the last case and execute the corresponding expression. If no pattern matches, the program will abort. | ||
Match expressions have *first match semantics*. They will try to find the first matching pattern sequentially from the first case to the last case and execute the corresponding matched expression. If no pattern matches, the program will abort. | ||
|
||
The match expression has a `Int` return value because all the case result in same value type `Int`. | ||
|
||
Patterns can be nested. If you don't care about the data associated with the enum constructor, you can use the *any pattern*, written as `_`, instead of introducing a new variable. It means discarding that value. | ||
The match expression has an `Int` return value because all the cases result in the same value type `Int`. | ||
|
||
Patterns can be nested. If you don't care about the data associated with the enum constructor, you can use the *any pattern*, written as `_`, instead of introducing a new variable. | ||
The underscore means that the value is discarded. |
3 changes: 1 addition & 2 deletions
3
moonbit-tour/tour/chapter3_pattern_matching/lesson3_constant_pattern/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# Constant pattern | ||
|
||
Almost all constant in MoonBit can be represented as a constant pattern. | ||
|
||
Almost all constants in MoonBit can be represented as a constant pattern. |
4 changes: 2 additions & 2 deletions
4
moonbit-tour/tour/chapter3_pattern_matching/lesson4_tuple_pattern/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 3 additions & 7 deletions
10
moonbit-tour/tour/chapter3_pattern_matching/lesson6_array_pattern/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
# Array Pattern | ||
# Array Pattern | ||
|
||
Array pattern is a sequence of patterns enclosed in `[]` that matches an array. | ||
An array pattern is a sequence of patterns enclosed in `[]` that matches an array. | ||
|
||
You can use `..` to match the rest of the array at the start or end, or the middle elements of the array. | ||
You can use `..` to match the rest of the array at the start, end, or middle elements of the array. | ||
|
||
In an array pattern, the `..` part can be bound to a new variable via an *alias pattern*. The type of that variable is `ArrayView`. The `sum` function uses this feature to calculate the sum of the array recursively. | ||
|
||
|
||
|
||
|
Oops, something went wrong.