Skip to content

Commit

Permalink
Merge pull request #136 from tonyfettes/var-to-let-mut
Browse files Browse the repository at this point in the history
var -> let mut
  • Loading branch information
bobzhang authored Jan 11, 2024
2 parents 9c75588 + c51c6af commit 4ad9b5e
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 104 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ while x == y {
The `while` statement doesn't yield anything; it only evaluates to `()` of unit type. MoonBit also provides the `break` and `continue` statements for controlling the flow of a loop.

```rust
var i = 0
var n = 0
let mut i = 0
let mut n = 0

while i < 10 {
i = i + 1
Expand All @@ -214,7 +214,7 @@ println(n) // outputs 25
The `while` loop can have an optional "continue" block after the loop condition, separated by comma. It is executed _after_ the body of every iteration, _before_ the condition of next iteration:

```rust
var i = 0
let mut i = 0
while i < 10, i = i + 1 {
println(i)
} // outputs 0 to 9
Expand All @@ -223,7 +223,7 @@ while i < 10, i = i + 1 {
If there are multiple statements in the continue block, they must be wrapped in braces. `continue` statement in the loop body will not skip continue block. For example, the following code will output all odd numbers smaller than 10:

```rust
var i = 1
let mut i = 1
while i < 10, i = i + 1 {
if (i % 2 == 0) {
continue
Expand Down Expand Up @@ -347,7 +347,7 @@ A variable can be declared as mutable or immutable using the keywords `var` or `
let zero = 0

fn init {
var i = 10
let mut i = 10
i = 20
print(i + zero)
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/fibonacci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func fib(num : Int) -> Int {
}
pub func test(n : Int, count : Int) -> Int {
var i = 0
var res = 0
let mut i = 0
let mut res = 0
while i < count {
res = fib(n)
i = i + 1
Expand Down
4 changes: 2 additions & 2 deletions examples/avl_tree/lib/avl.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub fn mem[U:Compare](self: T[U], x: U) -> Bool {
}

fn repeat_str(s: String, n: Int) -> String {
var result = ""
var i = 0
let mut result = ""
let mut i = 0
while i < n {
result = result + s
i = i + 1
Expand Down
8 changes: 4 additions & 4 deletions examples/avl_tree/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fn init {
var v : @lib.T[Int] = Empty // Create an empty AVL tree with Int type
let mut v : @lib.T[Int] = Empty // Create an empty AVL tree with Int type
let iter = 30

// Add values from 0 to iter-1 to the AVL tree
var i = 0
let mut i = 0
while i < iter {
v = v.add(i)
i = i + 1
Expand All @@ -15,7 +15,7 @@ fn init {
v.print_tree()

// Check values from 0 to iter-1 in the AVL tree
var j = 0
let mut j = 0
while j < iter {
if not(v.mem(j)) {
println("impossible")
Expand All @@ -24,7 +24,7 @@ fn init {
}

// Remove values from 0 to iter-1 from the AVL tree
var k = 0
let mut k = 0
while k < iter {
v = v.remove(k)
k = k + 1
Expand Down
8 changes: 4 additions & 4 deletions examples/buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn expand_size[T : Default](self : Buffer[T]) {
}
self.cap = new_capacity
let new_data = Array::make(new_capacity, T::default())
var index = 0
let mut index = 0
while index < self.len {
new_data[index] = self.data[index]
index = index + 1
Expand All @@ -49,7 +49,7 @@ pub fn append[T : Default](self : Buffer[T], value : T) {
}

pub fn truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
var index = 0
let mut index = 0
while index < another.len {
if self.len >= self.cap {
self.expand_size()
Expand All @@ -61,7 +61,7 @@ pub fn truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
}

pub fn clear[T : Default](self : Buffer[T]) {
var index = 0
let mut index = 0
while index < self.len {
self.data[index] = T::default()
index = index + 1
Expand All @@ -76,7 +76,7 @@ pub fn reset[T : Default](self : Buffer[T], capacity : Int) {
}

pub fn println[T : Show](self : Buffer[T]) {
var index = 0
let mut index = 0
print('[')
while index < self.len {
print(self.data[index].to_string())
Expand Down
2 changes: 1 addition & 1 deletion examples/buffer/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn init {
let buf : @lib.Buffer[Int] = @lib.Buffer::new(5)
println(buf.capacity()) // 5
var index = 0
let mut index = 0
while index < 8 {
buf.append(index)
index = index + 1
Expand Down
6 changes: 3 additions & 3 deletions examples/bytes-buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn expand_size(self : Buffer) {
(self.data.length() + 1) * 2
}
let new_data = Bytes::make(new_capacity, 0)
var index = 0
let mut index = 0
while index < self.len {
new_data[index] = self.data[index]
index = index + 1
Expand All @@ -55,7 +55,7 @@ pub fn append_int(self : Buffer, value : Int) {
}

pub fn truncate(self : Buffer, another: Buffer) {
var index = 0
let mut index = 0
while index < another.len {
if self.len >= self.data.length() {
self.expand_size()
Expand All @@ -67,7 +67,7 @@ pub fn truncate(self : Buffer, another: Buffer) {
}

pub fn clear(self : Buffer) {
var index = 0
let mut index = 0
while index < self.len {
self.data[index] = 0
index = index + 1
Expand Down
8 changes: 4 additions & 4 deletions examples/game_of_life/lib/draw.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ let cell_size = 5
pub fn draw_grid(canvas : Canvas_ctx) {
canvas.begin_path()
canvas.set_stroke_color(0xcccccc)
var i = 0
let mut i = 0
while i <= 64 {
canvas.move_to(i * (cell_size + 1) + 1, 0)
canvas.line_to(i * (cell_size + 1) + 1, (cell_size + 1) * 64 + 1)
i = i + 1
}
var j = 0
let mut j = 0
while j <= 64 {
canvas.move_to(0, j * (cell_size + 1) + 1)
canvas.line_to((cell_size + 1) * 64 + 1, j * (cell_size + 1) + 1)
Expand All @@ -37,9 +37,9 @@ pub fn draw_grid(canvas : Canvas_ctx) {
pub fn draw_cell(canvas : Canvas_ctx, universe : Universe, dead : Int,
alive : Int) {
canvas.begin_path()
var row = 0
let mut row = 0
while row < 64 {
var col = 0
let mut col = 0
while col < 64 {
let idx = universe.get_index(row, col)
let color = if universe.get_cell(idx) == 0 {
Expand Down
18 changes: 9 additions & 9 deletions examples/game_of_life/lib/game_of_life.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn new() -> Universe {
let width = 64
let height = 64
let cells : Array[Cell] = Array::make(width * height, Dead)
var idx = 0
let mut idx = 0
while idx < width * height {
if idx % 2 == 0 || idx % 7 == 0 {
cells[idx] = Alive
Expand Down Expand Up @@ -53,12 +53,12 @@ pub fn get_cell(self : Universe, idx : Int) -> Int {
}

fn live_neighbor_count(self : Universe, row : Int, column : Int) -> Int {
var count = 0
let mut count = 0
let delta_rows = [self.height - 1, 0, 1]
let delta_cols = [self.width - 1, 0, 1]
var r = 0
let mut r = 0
while r < 3 {
var c = 0
let mut c = 0
while c < 3 {
if delta_rows[r] == 0 && delta_cols[c] == 0 {
c = c + 1
Expand All @@ -77,9 +77,9 @@ fn live_neighbor_count(self : Universe, row : Int, column : Int) -> Int {

pub fn tick(self : Universe) {
let next : Array[Cell] = Array::make(self.width * self.height, Dead)
var r = 0
let mut r = 0
while r < self.height {
var c = 0
let mut c = 0
while c < self.width {
let idx = self.get_index(r, c)
let cell = self.cells[idx]
Expand All @@ -105,10 +105,10 @@ pub fn tick(self : Universe) {
}

pub fn to_string(self : Universe) -> String {
var r = 0
var cells = ""
let mut r = 0
let mut cells = ""
while r < self.height {
var c = 0
let mut c = 0
while c < self.width {
let idx = self.get_index(r, c)
let cell = self.cells[idx]
Expand Down
24 changes: 12 additions & 12 deletions examples/mandelbrot/lib/mandelbrot.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ let roffset : Double = if floor(image_height) % 2 == 0 {
}

pub fn iter(cx : Double, cy : Double) -> Double {
var x = 0.0
var y = 0.0
var newx = 0.0
var newy = 0.0
var smodz = 0.0
var i = 0
let mut x = 0.0
let mut y = 0.0
let mut newx = 0.0
let mut newy = 0.0
let mut smodz = 0.0
let mut i = 0
while i < max_iter_number {
newx = x * x - y * y + cx
newy = 2.0 * x * y + cy
Expand Down Expand Up @@ -70,7 +70,7 @@ fn interpolation(f : Double, c0 : Int, c1 : Int) -> Int {

pub fn get_color(d : Double) -> Int {
if d >= 0.0 {
var k = 0.021 * (d - 1.0 + log(log(128.0)) / log(2.0))
let mut k = 0.021 * (d - 1.0 + log(log(128.0)) / log(2.0))
k = log(1.0 + k) - 29.0 / 400.0
k = k - float_of_int(floor(k))
k = k * 400.0
Expand All @@ -95,12 +95,12 @@ pub fn calc_color(col : Int, row : Int, ox : Double, oy : Double,
let pixel_size = width / image_width
let cx = (float_of_int(col) - coffset) * pixel_size + ox
let cy = (float_of_int(row) - roffset) * pixel_size + oy
var r = 0
var g = 0
var b = 0
var i = -1
let mut r = 0
let mut g = 0
let mut b = 0
let mut i = -1
while i <= 1 {
var j = -1
let mut j = -1
while j <= 1 {
let d = iter(
cx + float_of_int(i) * pixel_size / 3.0,
Expand Down
6 changes: 3 additions & 3 deletions examples/number/lib/number.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ pub fn parse_int(s : String) -> Option[Int] {
let char_0 = 0x30
let char_u_a = 0x41
let char_a = 0x61
var index = 0
var is_negative = false
var result_digit = 0
let mut index = 0
let mut is_negative = false
let mut result_digit = 0
if s[index] == '-' {
index = index + 1
is_negative = true
Expand Down
2 changes: 1 addition & 1 deletion examples/palindrome_string/lib/str_iter.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub fn is_palindrome(chars : String) -> Bool {
let n = chars.length()
var i = 0
let mut i = 0
while i < n / 2 {
if chars[i] != chars[n - i - 1] {
return false
Expand Down
8 changes: 4 additions & 4 deletions examples/path/lib/path.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub fn parent(self : Path) -> String {
match self.platform {
Posix => {
let (parts, l) = split(self.inner, '/')
var parent = ""
var index = 0
let mut parent = ""
let mut index = 0
let end = if parts[l] == "" {
l - 1
} else {
Expand All @@ -40,8 +40,8 @@ pub fn parent(self : Path) -> String {
}
Windows => {
let (parts, l) = split(self.inner, '\\')
var parent = ""
var index = 0
let mut parent = ""
let mut index = 0
let end = if parts[l] == "" {
l - 1
} else {
Expand Down
Loading

0 comments on commit 4ad9b5e

Please sign in to comment.