From 8bf582d6ec6290df6c6252ddfbb5a04d2a093e4c Mon Sep 17 00:00:00 2001 From: Eric Johnson Date: Mon, 8 Apr 2024 17:58:27 -0700 Subject: [PATCH 1/2] Edit for statement section, add some examples --- content/docs/overview.md | 68 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/content/docs/overview.md b/content/docs/overview.md index 1719800e..170d62f2 100644 --- a/content/docs/overview.md +++ b/content/docs/overview.md @@ -292,18 +292,29 @@ where `a..=b` denotes a closed interval `[a,b]`, i.e. the upper limit is *inclus Certain built-in types can be iterated over: ```odin +some_string := "Hello, 世界" for character in some_string { fmt.println(character) } + +some_array := [3]int{1, 4, 9} for value in some_array { fmt.println(value) } + +some_slice := []int{1, 4, 9} for value in some_slice { fmt.println(value) } + +some_dynamic_array := [dynamic]int{1, 4, 9} +defer delete(some_dynamic_array) for value in some_dynamic_array { fmt.println(value) } + +some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4} +defer delete(some_map) for key in some_map { fmt.println(key) } @@ -329,9 +340,17 @@ for key, value in some_map { ``` The iterated values are *copies* and cannot be written to. -**Note:** When iterating across a string, the characters will be `rune`s and not bytes. `for in` assumes the string is encoded as UTF-8. +When iterating a string, the characters will be `rune`s and not bytes. `for in` assumes the string is encoded as UTF-8. + +```odin +str: string = "Some text" +for character in str { + assert(type_of(character) == rune) + fmt.println(character) +} +``` -It is also possible to iterate over arrays and slices in a by-reference manner by prepending a `&` to the value: +You can iterate arrays and slices by-reference with the [address operator](#address-operator): ```odin for &value in some_array { value = something @@ -348,12 +367,19 @@ for &value, index in some_dynamic_array { } ``` -Maps can have their *values* iterated in a by-reference manner but not their keys which are immutable: +Map values can be iterated by-reference, but their keys cannot since map keys are immutable: ```odin +some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4} +defer delete(some_map) + for key, &value in some_map { - value = something + value += 1 } + +fmt.println(some_map["A"]) // 2 +fmt.println(some_map["C"]) // 10 +fmt.println(some_map["B"]) // 5 ``` @@ -361,7 +387,7 @@ for key, &value in some_map { #### `for` reverse iteration -Recently a special directive was added which allows to `#reverse` the above mentioned range based iteration. +The `#reverse` directive makes a range-based `for` loop iterate in reverse. ```odin array := [?]int { 10, 20, 30, 40, 50 } @@ -371,6 +397,38 @@ array := [?]int { 10, 20, 30, 40, 50 } } ``` +#### `for` loop unrolling + +The `#unroll` directive unrolls a ranged-based `for` loop. + +```odin +// Ranges +#unroll for x, i in 1..<4 { + fmt.println(x, i) +} + +// Strings +#unroll for r, i in "Hello, unroll" { + fmt.println(r, i) +} + +// Arrays +#unroll for elem, idx in ([4]int{1, 4, 9, 16}) { + fmt.println(elem, idx) +} + +Foo_Enum :: enum { + A = 1, + B, + C = 6, + D, +} +// Enums +#unroll for elem, idx in Foo_Enum { + fmt.println(elem, idx) +} +``` + ### `if` statement Odin's `if` statements do not need to be surrounded by parentheses `( )` but braces `{ }` or `do` are required. From 3fe1555d5abe572ba01726102ac39a71c2c60f60 Mon Sep 17 00:00:00 2001 From: Eric Johnson Date: Tue, 30 Apr 2024 13:55:53 -0700 Subject: [PATCH 2/2] remove loop unrolling --- content/docs/overview.md | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/content/docs/overview.md b/content/docs/overview.md index 170d62f2..56621304 100644 --- a/content/docs/overview.md +++ b/content/docs/overview.md @@ -397,38 +397,6 @@ array := [?]int { 10, 20, 30, 40, 50 } } ``` -#### `for` loop unrolling - -The `#unroll` directive unrolls a ranged-based `for` loop. - -```odin -// Ranges -#unroll for x, i in 1..<4 { - fmt.println(x, i) -} - -// Strings -#unroll for r, i in "Hello, unroll" { - fmt.println(r, i) -} - -// Arrays -#unroll for elem, idx in ([4]int{1, 4, 9, 16}) { - fmt.println(elem, idx) -} - -Foo_Enum :: enum { - A = 1, - B, - C = 6, - D, -} -// Enums -#unroll for elem, idx in Foo_Enum { - fmt.println(elem, idx) -} -``` - ### `if` statement Odin's `if` statements do not need to be surrounded by parentheses `( )` but braces `{ }` or `do` are required.