vfmt: vfmt examples/*.v

pull/8935/head
Delyan Angelov 2021-02-23 19:43:44 +02:00
parent 0fa2f6d52c
commit 9e06af8bf9
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
18 changed files with 191 additions and 162 deletions

View File

@ -25,14 +25,22 @@ 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} }
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,7 +49,9 @@ 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 }
Empty {
return false
}
Node {
return if x == tree.value {
true
@ -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)}
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 {
if x == tree.value { return tree.right } else { return Node{...tree, right: delete(tree.right, x)} }
Node{
...tree
left: delete(tree.left, x)
}
}
} else {
if x == tree.value {
return tree.right
} else {
return Node{
...tree
right: delete(tree.right, x)
}
}
}
}
}

View File

@ -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')
}
}

View File

@ -10,4 +10,3 @@ fn main() {
t := time.unix(resp.text.int())
println(t.format())
}

View File

@ -1,5 +1,4 @@
// This program displays the fibonacci sequence
import os
fn main() {

View File

@ -9,8 +9,7 @@ fn main() {
}
if s == '' {
println(n)
}
else {
} else {
println(s)
}
s = ''

View File

@ -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')

View File

@ -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++

View File

@ -2,7 +2,6 @@
// Output:
// -0.169075164
// -0.169059907
import math
const (
@ -86,7 +85,8 @@ fn offsetmomentum(mut sys System) {
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)
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

View File

@ -11,8 +11,6 @@ fn send_request(mut wg sync.WaitGroup) ?string {
return data.text
}
fn main() {
mut wg := sync.new_waitgroup()
for i := 0; i < 50; i++ {

View File

@ -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))

View File

@ -17,7 +17,9 @@ fn main() {
}
fn quick_sort<T>(mut arr []T, l int, r int) {
if l>=r { return }
if l >= r {
return
}
mut sep := l // what is sep: [...all_value<arr[sep]...sep...all_value>=arr[sep]...]
for i in l + 1 .. r + 1 {
if arr[i] < arr[l] {

View File

@ -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++
}

View File

@ -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')
}

View File

@ -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 {}