Skip to content

Commit

Permalink
tour: add range, range pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed Dec 25, 2024
1 parent 41634d5 commit 241c01a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
fn main {
println("\nfor-in loop:")
println("for-in loop:")
let array = [1, 2, 3]
for element in array {
println("element: \{element}")
}

println("for-in loop with index:")
for i, element in array {
println("index: \{i}, element: \{element}")
}

println("\nfor-in loop for map:")
println("for-in loop for map:")
let map = { "key1": 1, "key2": 2, "key3": 3 }
for k, v in map {
println("key: \{k}, value: \{v}")
Expand Down
14 changes: 14 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson12_range/index.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main {
println("number 1 to 3:")
for i in 1..<4 {
println(i)
}

println("number 1 to 4:")
for i in 1..=4 {
println(i)
}
}



7 changes: 7 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson12_range/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Range

You can also use *range* in a for-in loop.

- `start..<end` range is inclusive of the start value and exclusive of the end value.
- `start..=end` range is inclusive of both the start and end values.

11 changes: 11 additions & 0 deletions moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type UserId Int
type UserName String

fn main {
let user_id : UserId = UserId(1)
let user_name : UserName = UserName("Alice")
let UserId(id) = user_id
let UserName(name) = user_name
println(user_id._)
println(user_name._)
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
fn score_to_grade(score : Int) -> String {
match score {
0..<60 => "F"
60..<70 => "D"
70..<80 => "C"
80..<90 => "B"
90..=100 => "A"
_ => "Invalid score"
}
}

fn classify_char(c : Char) -> String {
match c {
'A'..='Z' => "UpperCase"
'a'..='z' => "LowerCase"
'0'..='9' => "Digit"
_ => "Special"
}
}

fn is_scalar_value(codepoint : Int) -> Bool {
match codepoint {
0x0000..=0xD7FF | 0xE000..=0x10FFFF => true
_ => false
}
}

fn main {
println(score_to_grade(50))
println(score_to_grade(85))
println(score_to_grade(95))
println(classify_char('A'))
println(classify_char('1'))
println(classify_char('!'))
println(is_scalar_value(0xD500))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Range Pattern

For consecutive values, using the previously introduced *or-patterns* can be somewhat cumbersome. To address this issue, we can use *range patterns*. *Range patterns* can match values within a specified range.


Recall the syntax we learned in Chapter 1:

- `start..<end` range is inclusive of the start value and exclusive of the end value.
- `start..=end` range is inclusive of both the start and end values.


Range patterns support built-in integer-like types, such as `Byte`, `Int`, `UInt`, `Int64`, `UInt64`, and `Char`.

0 comments on commit 241c01a

Please sign in to comment.