From 9e06af8bf9da42c6277ce1c48691c9162775afbd Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 23 Feb 2021 19:43:44 +0200 Subject: [PATCH] vfmt: vfmt examples/*.v --- examples/binary_search_tree.v | 74 ++++++++++++---- examples/cli.v | 14 +-- examples/fetch.v | 13 ++- examples/fibonacci.v | 5 +- examples/fizz_buzz.v | 3 +- examples/json.v | 4 +- examples/log.v | 2 +- examples/mini_calculator.v | 2 +- examples/nbody.v | 162 +++++++++++++++++----------------- examples/net_peer_ip.v | 4 +- examples/net_t.v | 26 +++--- examples/path_tracing.v | 7 +- examples/quick_sort.v | 20 +++-- examples/random_ips.v | 2 +- examples/terminal_control.v | 8 +- examples/vmod.v | 4 +- examples/vpwgen.v | 2 +- examples/wkhtmltopdf.v | 1 + 18 files changed, 191 insertions(+), 162 deletions(-) diff --git a/examples/binary_search_tree.v b/examples/binary_search_tree.v index 5b7dc24f34..34dbe6ab13 100644 --- a/examples/binary_search_tree.v +++ b/examples/binary_search_tree.v @@ -25,15 +25,23 @@ fn size(tree Tree) int { // insert a value to BST fn insert(tree Tree, x f64) Tree { match tree { - Empty { return Node{x, tree, tree} } - Node { + Empty { + return Node{x, tree, tree} + } + Node { return if x == tree.value { tree } else if x < tree.value { - Node{...tree, left: insert(tree.left, x)} + Node{ + ...tree + left: insert(tree.left, x) + } } else { - Node{...tree, right: insert(tree.right, x)} - } + Node{ + ...tree + right: insert(tree.right, x) + } + } } } } @@ -41,15 +49,17 @@ fn insert(tree Tree, x f64) Tree { // whether able to find a value in BST fn search(tree Tree, x f64) bool { match tree { - Empty { return false } - Node { + Empty { + return false + } + Node { return if x == tree.value { true } else if x < tree.value { search(tree.left, x) } else { search(tree.right, x) - } + } } } } @@ -65,20 +75,46 @@ fn min(tree Tree) f64 { // delete a value in BST (if nonexistant do nothing) fn delete(tree Tree, x f64) Tree { match tree { - Empty { return tree } + Empty { + return tree + } Node { if tree.left is Node && tree.right is Node { - return if x < tree.value { - Node{...tree, left: delete(tree.left, x)} + return if x < tree.value { + Node{ + ...tree + left: delete(tree.left, x) + } } else if x > tree.value { - Node{...tree, right: delete(tree.right, x)} + Node{ + ...tree + right: delete(tree.right, x) + } } else { - Node{...tree, value: min(tree.right), right: delete(tree.right, min(tree.right))} - } + Node{ + ...tree + value: min(tree.right) + right: delete(tree.right, min(tree.right)) + } + } } else if tree.left is Node { - return if x == tree.value { tree.left } else { Node{...tree, left: delete(tree.left, x)} } + return if x == tree.value { + tree.left + } else { + Node{ + ...tree + left: delete(tree.left, x) + } + } } else { - if x == tree.value { return tree.right } else { return Node{...tree, right: delete(tree.right, x)} } + if x == tree.value { + return tree.right + } else { + return Node{ + ...tree + right: delete(tree.right, x) + } + } } } } @@ -90,13 +126,13 @@ fn main() { for i in input { tree = insert(tree, i) } - println('[1] after insertion tree size is ${size(tree)}') // 11 + println('[1] after insertion tree size is ${size(tree)}') // 11 del := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] for i in del { tree = delete(tree, i) } - print('[2] after deletion tree size is ${size(tree)}, ') // 7 - print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0 + print('[2] after deletion tree size is ${size(tree)}, ') // 7 + print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0 for i in input { if !search(tree, i) { print('$i ') diff --git a/examples/cli.v b/examples/cli.v index 66a8eb21d1..e41aed436e 100644 --- a/examples/cli.v +++ b/examples/cli.v @@ -43,12 +43,8 @@ fn main() { } fn greet_func(cmd Command) ? { - language := cmd.flags.get_string('language') or { - panic('Failed to get `language` flag: $err') - } - times := cmd.flags.get_int('times') or { - panic('Failed to get `times` flag: $err') - } + language := cmd.flags.get_string('language') or { panic('Failed to get `language` flag: $err') } + times := cmd.flags.get_int('times') or { panic('Failed to get `times` flag: $err') } name := cmd.args[0] for _ in 0 .. times { match language { @@ -68,11 +64,9 @@ fn greet_func(cmd Command) ? { } } } - fun := cmd.flags.get_strings('fun') or { - panic('Failed to get `fun` flag: $err') - } + fun := cmd.flags.get_strings('fun') or { panic('Failed to get `fun` flag: $err') } for f in fun { - println('fun: ${f}') + println('fun: $f') } } diff --git a/examples/fetch.v b/examples/fetch.v index 7f2b8626e7..e1a7132acc 100644 --- a/examples/fetch.v +++ b/examples/fetch.v @@ -2,12 +2,11 @@ import time import net.http fn main() { - resp := http.get('https://vlang.io/utc_now') or { - println('failed to fetch data from the server') - return - } + resp := http.get('https://vlang.io/utc_now') or { + println('failed to fetch data from the server') + return + } - t := time.unix(resp.text.int()) - println(t.format()) + t := time.unix(resp.text.int()) + println(t.format()) } - diff --git a/examples/fibonacci.v b/examples/fibonacci.v index 3a439fbc8f..6244c96092 100644 --- a/examples/fibonacci.v +++ b/examples/fibonacci.v @@ -1,5 +1,4 @@ // This program displays the fibonacci sequence - import os fn main() { @@ -10,7 +9,7 @@ fn main() { // Exit return } - + // Parse first argument and cast it to int stop := os.args[1].int() @@ -25,7 +24,7 @@ fn main() { mut b := u64(0) mut c := u64(1) - for _ in 0..stop { + for _ in 0 .. stop { // Set a and b to the next term a = b b = c diff --git a/examples/fizz_buzz.v b/examples/fizz_buzz.v index 4722cbf3a5..3310460871 100644 --- a/examples/fizz_buzz.v +++ b/examples/fizz_buzz.v @@ -9,8 +9,7 @@ fn main() { } if s == '' { println(n) - } - else { + } else { println(s) } s = '' diff --git a/examples/json.v b/examples/json.v index 6c1a6e45b1..841d9e22cf 100644 --- a/examples/json.v +++ b/examples/json.v @@ -1,8 +1,8 @@ import json struct User { - name string - age int + name string + age int mut: is_registered bool } diff --git a/examples/log.v b/examples/log.v index db82119b78..11f5a2c348 100644 --- a/examples/log.v +++ b/examples/log.v @@ -5,7 +5,7 @@ fn main() { l.set_level(.info) // Make a new file called info.log in the current folder l.set_full_logpath('./info.log') - println('Please check the file: ${l.output_file_name} after this example crashes.') + println('Please check the file: $l.output_file_name after this example crashes.') l.info('info') l.warn('warn') diff --git a/examples/mini_calculator.v b/examples/mini_calculator.v index 5f79f9d476..4663f4c545 100644 --- a/examples/mini_calculator.v +++ b/examples/mini_calculator.v @@ -113,7 +113,7 @@ fn is_num_string(str string) bool { fn main() { println('Please enter the expression you want to calculate, e.g. 1e2+(3-2.5)*6/1.5 .') - println("Enter \'exit\' or \'EXIT\' to quit.") + println("Enter 'exit' or 'EXIT' to quit.") mut expr_count := 0 for { expr_count++ diff --git a/examples/nbody.v b/examples/nbody.v index 37ae66748c..0f719e81a9 100644 --- a/examples/nbody.v +++ b/examples/nbody.v @@ -2,127 +2,127 @@ // Output: // -0.169075164 // -0.169059907 - import math const ( - solar_mass = 39.47841760435743197 //4.0 * math.Pi * math.Pi - days_per_year = 365.24 - c_n = 5 + solar_mass = 39.47841760435743197 // 4.0 * math.Pi * math.Pi + days_per_year = 365.24 + c_n = 5 ) struct Position { pub mut: - x f64 - y f64 - z f64 + x f64 + y f64 + z f64 } struct Momentum { pub mut: - x f64 - y f64 - z f64 - m f64 + x f64 + y f64 + z f64 + m f64 } struct System { pub mut: - v []Momentum - s []Position + v []Momentum + s []Position } fn advance(mut sys System, dt f64) { - for i in 0..c_n - 1 { - mut vx := sys.v[i].x - mut vy := sys.v[i].y - mut vz := sys.v[i].z + for i in 0 .. c_n - 1 { + mut vx := sys.v[i].x + mut vy := sys.v[i].y + mut vz := sys.v[i].z - for j := i + 1; j < c_n; j++ { - dx := sys.s[i].x - sys.s[j].x - dy := sys.s[i].y - sys.s[j].y - dz := sys.s[i].z - sys.s[j].z + for j := i + 1; j < c_n; j++ { + dx := sys.s[i].x - sys.s[j].x + dy := sys.s[i].y - sys.s[j].y + dz := sys.s[i].z - sys.s[j].z - dsquared := dx * dx + dy * dy + dz * dz - distance := math.sqrt(dsquared) - mag := (dt / (dsquared * distance)) - mi := sys.v[i].m + dsquared := dx * dx + dy * dy + dz * dz + distance := math.sqrt(dsquared) + mag := (dt / (dsquared * distance)) + mi := sys.v[i].m - vx -= dx * sys.v[j].m * mag - vy -= dy * sys.v[j].m * mag - vz -= dz * sys.v[j].m * mag + vx -= dx * sys.v[j].m * mag + vy -= dy * sys.v[j].m * mag + vz -= dz * sys.v[j].m * mag - sys.v[j].x += dx * mi * mag - sys.v[j].y += dy * mi * mag - sys.v[j].z += dz * mi * mag - } - sys.v[i].x = vx - sys.v[i].y = vy - sys.v[i].z = vz - } + sys.v[j].x += dx * mi * mag + sys.v[j].y += dy * mi * mag + sys.v[j].z += dz * mi * mag + } + sys.v[i].x = vx + sys.v[i].y = vy + sys.v[i].z = vz + } - for i in 0..c_n { - sys.s[i].x += dt * sys.v[i].x - sys.s[i].y += dt * sys.v[i].y - sys.s[i].z += dt * sys.v[i].z - } + for i in 0 .. c_n { + sys.s[i].x += dt * sys.v[i].x + sys.s[i].y += dt * sys.v[i].y + sys.s[i].z += dt * sys.v[i].z + } } fn offsetmomentum(mut sys System) { - mut px := f64(0) - mut py := f64(0) - mut pz := f64(0) + mut px := f64(0) + mut py := f64(0) + mut pz := f64(0) - for i in 0..c_n { - px += sys.v[i].x * sys.v[i].m - py += sys.v[i].y * sys.v[i].m - pz += sys.v[i].z * sys.v[i].m - } - sys.v[0].x = -px / solar_mass - sys.v[0].y = -py / solar_mass - sys.v[0].z = -pz / solar_mass + for i in 0 .. c_n { + px += sys.v[i].x * sys.v[i].m + py += sys.v[i].y * sys.v[i].m + pz += sys.v[i].z * sys.v[i].m + } + sys.v[0].x = -px / solar_mass + sys.v[0].y = -py / solar_mass + sys.v[0].z = -pz / solar_mass } fn energy(sys System) f64 { - mut e := f64(0) - for i in 0..c_n { - e += 0.5 * sys.v[i].m * (sys.v[i].x * sys.v[i].x + sys.v[i].y * sys.v[i].y + sys.v[i].z * sys.v[i].z) - for j := i + 1; j < c_n; j++ { - dx := sys.s[i].x - sys.s[j].x - dy := sys.s[i].y - sys.s[j].y - dz := sys.s[i].z - sys.s[j].z - distance := math.sqrt(dx * dx + dy * dy + dz * dz) - e -= (sys.v[i].m * sys.v[j].m) / distance - } - } - return e + mut e := f64(0) + for i in 0 .. c_n { + e += 0.5 * sys.v[i].m * (sys.v[i].x * sys.v[i].x + sys.v[i].y * sys.v[i].y + + sys.v[i].z * sys.v[i].z) + for j := i + 1; j < c_n; j++ { + dx := sys.s[i].x - sys.s[j].x + dy := sys.s[i].y - sys.s[j].y + dz := sys.s[i].z - sys.s[j].z + distance := math.sqrt(dx * dx + dy * dy + dz * dz) + e -= (sys.v[i].m * sys.v[j].m) / distance + } + } + return e } fn arr_momentum() []Momentum { - return [ - Momentum {0.0, 0.0, 0.0, solar_mass}, - Momentum {1.66007664274403694e-03 * days_per_year, 7.69901118419740425e-03 * days_per_year, -6.90460016972063023e-05 * days_per_year, 9.54791938424326609e-04 * solar_mass}, - Momentum {-2.76742510726862411e-03 * days_per_year, 4.99852801234917238e-03 * days_per_year, 2.30417297573763929e-05 * days_per_year, 2.85885980666130812e-04 * solar_mass}, - Momentum {2.96460137564761618e-03 * days_per_year, 2.37847173959480950e-03 * days_per_year, -2.96589568540237556e-05 * days_per_year, 4.36624404335156298e-05 * solar_mass}, - Momentum {2.68067772490389322e-03 * days_per_year, 1.62824170038242295e-03 * days_per_year, -9.51592254519715870e-05 * days_per_year, 5.15138902046611451e-05 * solar_mass}, - ] + return [ + Momentum{0.0, 0.0, 0.0, solar_mass}, + Momentum{1.66007664274403694e-03 * days_per_year, 7.69901118419740425e-03 * days_per_year, -6.90460016972063023e-05 * days_per_year, 9.54791938424326609e-04 * solar_mass}, + Momentum{-2.76742510726862411e-03 * days_per_year, 4.99852801234917238e-03 * days_per_year, 2.30417297573763929e-05 * days_per_year, 2.85885980666130812e-04 * solar_mass}, + Momentum{2.96460137564761618e-03 * days_per_year, 2.37847173959480950e-03 * days_per_year, -2.96589568540237556e-05 * days_per_year, 4.36624404335156298e-05 * solar_mass}, + Momentum{2.68067772490389322e-03 * days_per_year, 1.62824170038242295e-03 * days_per_year, -9.51592254519715870e-05 * days_per_year, 5.15138902046611451e-05 * solar_mass}, + ] } fn arr_position() []Position { - return [ - Position {0.0, 0.0, 0.0}, - Position {4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01}, - Position {8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01}, - Position {1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01}, - Position {1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01}, - ] + return [ + Position{0.0, 0.0, 0.0}, + Position{4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01}, + Position{8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01}, + Position{1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01}, + Position{1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01}, + ] } fn main() { - mut sys := &System {arr_momentum(), arr_position()} + mut sys := &System{arr_momentum(), arr_position()} offsetmomentum(mut sys) - println('${energy(sys):.9f}') //-0.169075164 - for _ in 0..50_000_000 { + println('${energy(sys):.9f}') //-0.169075164 + for _ in 0 .. 50_000_000 { advance(mut sys, 0.01) } println('${energy(sys):.9f}') //-0.169059907 diff --git a/examples/net_peer_ip.v b/examples/net_peer_ip.v index 9c45c08b94..7f84deb98f 100644 --- a/examples/net_peer_ip.v +++ b/examples/net_peer_ip.v @@ -1,5 +1,5 @@ import net -conn := net.dial_tcp('google.com:80')? -peer_addr := conn.peer_addr()? +conn := net.dial_tcp('google.com:80') ? +peer_addr := conn.peer_addr() ? println('$peer_addr') diff --git a/examples/net_t.v b/examples/net_t.v index 6ae4b7520c..8e8255a8d1 100644 --- a/examples/net_t.v +++ b/examples/net_t.v @@ -3,21 +3,19 @@ import sync import time fn send_request(mut wg sync.WaitGroup) ?string { - start := time.ticks() - data := http.get('https://google.com')? - finish := time.ticks() - println('Finish getting time ${finish - start} ms') - wg.done() - return data.text + start := time.ticks() + data := http.get('https://google.com') ? + finish := time.ticks() + println('Finish getting time ${finish - start} ms') + wg.done() + return data.text } - - fn main() { - mut wg := sync.new_waitgroup() - for i := 0; i < 50; i++ { - wg.add(1) - go send_request(mut wg) - } - wg.wait() + mut wg := sync.new_waitgroup() + for i := 0; i < 50; i++ { + wg.add(1) + go send_request(mut wg) + } + wg.wait() } diff --git a/examples/path_tracing.v b/examples/path_tracing.v index b07503499b..2c32ae96bc 100644 --- a/examples/path_tracing.v +++ b/examples/path_tracing.v @@ -474,8 +474,11 @@ fn radiance(r Ray, depthi int, scene_id int) Vec { mut tmp := Vec{} if depth > 2 { // Russian roulette - tmp = if rand_f64() < pp { radiance(refl_ray, depth, scene_id).mult_s(rp) } else { radiance(Ray{x, tdir}, - depth, scene_id).mult_s(tp) } + tmp = if rand_f64() < pp { + radiance(refl_ray, depth, scene_id).mult_s(rp) + } else { + radiance(Ray{x, tdir}, depth, scene_id).mult_s(tp) + } } else { tmp = (radiance(refl_ray, depth, scene_id).mult_s(re)) + (radiance(Ray{x, tdir}, depth, scene_id).mult_s(tr)) diff --git a/examples/quick_sort.v b/examples/quick_sort.v index 69c1675208..f890749997 100644 --- a/examples/quick_sort.v +++ b/examples/quick_sort.v @@ -7,32 +7,34 @@ const ( fn main() { mut arr := []int{} - for _ in 0..gen_len { + for _ in 0 .. gen_len { arr << rand.intn(gen_max) } println('length of random array is $arr.len') println('before quick sort whether array is sorted: ${is_sorted(arr)}') - quick_sort(mut arr, 0, arr.len-1) + quick_sort(mut arr, 0, arr.len - 1) println('after quick sort whether array is sorted: ${is_sorted(arr)}') } fn quick_sort(mut arr []T, l int, r int) { - if l>=r { return } - mut sep := l // what is sep: [...all_value=arr[sep]...] - for i in l+1..r+1 { + if l >= r { + return + } + mut sep := l // what is sep: [...all_value=arr[sep]...] + for i in l + 1 .. r + 1 { if arr[i] < arr[l] { sep++ arr[i], arr[sep] = arr[sep], arr[i] } } arr[l], arr[sep] = arr[sep], arr[l] - quick_sort(mut arr, l, sep-1) - quick_sort(mut arr, sep+1, r) + quick_sort(mut arr, l, sep - 1) + quick_sort(mut arr, sep + 1, r) } fn is_sorted(arr []T) bool { - for i in 0..arr.len-1 { - if arr[i] > arr[i+1] { + for i in 0 .. arr.len - 1 { + if arr[i] > arr[i + 1] { return false } } diff --git a/examples/random_ips.v b/examples/random_ips.v index 839306ddc2..59c4eff9cb 100644 --- a/examples/random_ips.v +++ b/examples/random_ips.v @@ -1,7 +1,7 @@ import rand fn main() { - for _ in 0..10 { + for _ in 0 .. 10 { println('${rand.intn(255)}.${rand.intn(255)}.${rand.intn(255)}.${rand.intn(255)}') } } diff --git a/examples/terminal_control.v b/examples/terminal_control.v index 5efb871045..404f290c1d 100644 --- a/examples/terminal_control.v +++ b/examples/terminal_control.v @@ -14,10 +14,10 @@ fn main() { fn sleeping_line(x int, y int, size int, ch string) { mut i := 0 for i < size { - term.set_cursor_position({ + term.set_cursor_position( x: x + i y: y - }) + ) print(term.bold(term.yellow(ch))) i++ } @@ -26,10 +26,10 @@ fn sleeping_line(x int, y int, size int, ch string) { fn standing_line(x int, y int, size int, ch string) { mut i := 0 for i < size { - term.set_cursor_position({ + term.set_cursor_position( x: x y: y + i - }) + ) print(term.bold(term.yellow(ch))) i++ } diff --git a/examples/vmod.v b/examples/vmod.v index 76ce046b9a..9e34b32851 100644 --- a/examples/vmod.v +++ b/examples/vmod.v @@ -3,9 +3,7 @@ module main import v.vmod fn main() { - mod := vmod.decode(@VMOD_FILE) or { - panic('Error decoding v.mod') - } + mod := vmod.decode(@VMOD_FILE) or { panic('Error decoding v.mod') } println('$mod.name has version $mod.version') println('\nThe full mod struct: \n$mod') } diff --git a/examples/vpwgen.v b/examples/vpwgen.v index 05563bda2a..2f803e4cec 100644 --- a/examples/vpwgen.v +++ b/examples/vpwgen.v @@ -10,7 +10,7 @@ fn main() { for _ in 0 .. repeats { mut sb := strings.new_builder(blocksize) for { - x := rand.read(blocksize)? + x := rand.read(blocksize) ? for c in x { if c >= `0` && c <= `~` { sb.write_b(c) diff --git a/examples/wkhtmltopdf.v b/examples/wkhtmltopdf.v index 1d92a33d69..49248569e3 100644 --- a/examples/wkhtmltopdf.v +++ b/examples/wkhtmltopdf.v @@ -12,6 +12,7 @@ import os // https://wkhtmltopdf.org/libwkhtmltox/ #flag -lwkhtmltox #include "wkhtmltox/pdf.h" # You can install the C package for your system from the wkhtmltopdf.org/downloads.html page + struct C.wkhtmltopdf_global_settings {} struct C.wkhtmltopdf_object_settings {}