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 // insert a value to BST
fn insert(tree Tree, x f64) Tree { fn insert(tree Tree, x f64) Tree {
match tree { match tree {
Empty { return Node{x, tree, tree} } Empty {
return Node{x, tree, tree}
}
Node { Node {
return if x == tree.value { return if x == tree.value {
tree tree
} else if x < tree.value { } else if x < tree.value {
Node{...tree, left: insert(tree.left, x)} Node{
...tree
left: insert(tree.left, x)
}
} else { } 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 // whether able to find a value in BST
fn search(tree Tree, x f64) bool { fn search(tree Tree, x f64) bool {
match tree { match tree {
Empty { return false } Empty {
return false
}
Node { Node {
return if x == tree.value { return if x == tree.value {
true true
@ -65,20 +75,46 @@ fn min(tree Tree) f64 {
// delete a value in BST (if nonexistant do nothing) // delete a value in BST (if nonexistant do nothing)
fn delete(tree Tree, x f64) Tree { fn delete(tree Tree, x f64) Tree {
match tree { match tree {
Empty { return tree } Empty {
return tree
}
Node { Node {
if tree.left is Node && tree.right is Node { if tree.left is Node && tree.right is Node {
return if x < tree.value { return if x < tree.value {
Node{...tree, left: delete(tree.left, x)} Node{
...tree
left: delete(tree.left, x)
}
} else if x > tree.value { } else if x > tree.value {
Node{...tree, right: delete(tree.right, x)} Node{
...tree
right: delete(tree.right, x)
}
} else { } 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 { } 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 { } 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) ? { fn greet_func(cmd Command) ? {
language := cmd.flags.get_string('language') or { language := cmd.flags.get_string('language') or { panic('Failed to get `language` flag: $err') }
panic('Failed to get `language` flag: $err') times := cmd.flags.get_int('times') or { panic('Failed to get `times` flag: $err') }
}
times := cmd.flags.get_int('times') or {
panic('Failed to get `times` flag: $err')
}
name := cmd.args[0] name := cmd.args[0]
for _ in 0 .. times { for _ in 0 .. times {
match language { match language {
@ -68,11 +64,9 @@ fn greet_func(cmd Command) ? {
} }
} }
} }
fun := cmd.flags.get_strings('fun') or { fun := cmd.flags.get_strings('fun') or { panic('Failed to get `fun` flag: $err') }
panic('Failed to get `fun` flag: $err')
}
for f in fun { for f in fun {
println('fun: ${f}') println('fun: $f')
} }
} }

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@ fn main() {
l.set_level(.info) l.set_level(.info)
// Make a new file called info.log in the current folder // Make a new file called info.log in the current folder
l.set_full_logpath('./info.log') 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.info('info')
l.warn('warn') l.warn('warn')

View File

@ -113,7 +113,7 @@ fn is_num_string(str string) bool {
fn main() { fn main() {
println('Please enter the expression you want to calculate, e.g. 1e2+(3-2.5)*6/1.5 .') 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 mut expr_count := 0
for { for {
expr_count++ expr_count++

View File

@ -2,7 +2,6 @@
// Output: // Output:
// -0.169075164 // -0.169075164
// -0.169059907 // -0.169059907
import math import math
const ( const (
@ -86,7 +85,8 @@ fn offsetmomentum(mut sys System) {
fn energy(sys System) f64 { fn energy(sys System) f64 {
mut e := f64(0) mut e := f64(0)
for i in 0 .. c_n { 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++ { for j := i + 1; j < c_n; j++ {
dx := sys.s[i].x - sys.s[j].x dx := sys.s[i].x - sys.s[j].x
dy := sys.s[i].y - sys.s[j].y 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 return data.text
} }
fn main() { fn main() {
mut wg := sync.new_waitgroup() mut wg := sync.new_waitgroup()
for i := 0; i < 50; i++ { 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{} mut tmp := Vec{}
if depth > 2 { if depth > 2 {
// Russian roulette // Russian roulette
tmp = if rand_f64() < pp { radiance(refl_ray, depth, scene_id).mult_s(rp) } else { radiance(Ray{x, tdir}, tmp = if rand_f64() < pp {
depth, scene_id).mult_s(tp) } radiance(refl_ray, depth, scene_id).mult_s(rp)
} else {
radiance(Ray{x, tdir}, depth, scene_id).mult_s(tp)
}
} else { } else {
tmp = (radiance(refl_ray, depth, scene_id).mult_s(re)) + tmp = (radiance(refl_ray, depth, scene_id).mult_s(re)) +
(radiance(Ray{x, tdir}, depth, scene_id).mult_s(tr)) (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) { 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]...] mut sep := l // what is sep: [...all_value<arr[sep]...sep...all_value>=arr[sep]...]
for i in l + 1 .. r + 1 { for i in l + 1 .. r + 1 {
if arr[i] < arr[l] { if arr[i] < arr[l] {

View File

@ -14,10 +14,10 @@ fn main() {
fn sleeping_line(x int, y int, size int, ch string) { fn sleeping_line(x int, y int, size int, ch string) {
mut i := 0 mut i := 0
for i < size { for i < size {
term.set_cursor_position({ term.set_cursor_position(
x: x + i x: x + i
y: y y: y
}) )
print(term.bold(term.yellow(ch))) print(term.bold(term.yellow(ch)))
i++ 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) { fn standing_line(x int, y int, size int, ch string) {
mut i := 0 mut i := 0
for i < size { for i < size {
term.set_cursor_position({ term.set_cursor_position(
x: x x: x
y: y + i y: y + i
}) )
print(term.bold(term.yellow(ch))) print(term.bold(term.yellow(ch)))
i++ i++
} }

View File

@ -3,9 +3,7 @@ module main
import v.vmod import v.vmod
fn main() { fn main() {
mod := vmod.decode(@VMOD_FILE) or { mod := vmod.decode(@VMOD_FILE) or { panic('Error decoding v.mod') }
panic('Error decoding v.mod')
}
println('$mod.name has version $mod.version') println('$mod.name has version $mod.version')
println('\nThe full mod struct: \n$mod') println('\nThe full mod struct: \n$mod')
} }

View File

@ -12,6 +12,7 @@ import os
// https://wkhtmltopdf.org/libwkhtmltox/ // https://wkhtmltopdf.org/libwkhtmltox/
#flag -lwkhtmltox #flag -lwkhtmltox
#include "wkhtmltox/pdf.h" # You can install the C package for your system from the wkhtmltopdf.org/downloads.html page #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_global_settings {}
struct C.wkhtmltopdf_object_settings {} struct C.wkhtmltopdf_object_settings {}