Skip to content

Commit 256d728

Browse files
authored
Improve V code (#376)
1 parent f08e01a commit 256d728

File tree

15 files changed

+47
-46
lines changed

15 files changed

+47
-46
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.v linguist-language=V

bench/algorithm/binarytrees/1.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() {
4242

4343
stretch_depth := max_depth + 1
4444
stretch_tree := create(stretch_depth)
45-
println('stretch tree of depth $stretch_depth\t check: ${check(stretch_tree)}')
45+
println('stretch tree of depth ${stretch_depth}\t check: ${check(stretch_tree)}')
4646

4747
long_lived_tree := create(max_depth)
4848

@@ -58,8 +58,8 @@ fn main() {
5858
check_result += check(node)
5959
}
6060

61-
println('$n\t trees of depth $depth\t check: $check_result')
61+
println('${n}\t trees of depth ${depth}\t check: ${check_result}')
6262
}
6363

64-
println('long lived tree of depth $max_depth\t check: ${check(long_lived_tree)}')
64+
println('long lived tree of depth ${max_depth}\t check: ${check(long_lived_tree)}')
6565
}

bench/algorithm/coro-prime-sieve/1.v

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ fn main() {
99
n = strconv.atoi(os.args[1]) or { n }
1010
}
1111

12-
mut ch := chan int{cap:1}
13-
go generate(ch)
12+
mut ch := chan int{cap: 1}
13+
spawn generate(ch)
1414
for _ in 0 .. n {
1515
prime := <-ch
1616
println(prime)
17-
ch_next := chan int{cap:1}
18-
go filter(ch, ch_next, prime)
17+
ch_next := chan int{cap: 1}
18+
spawn filter(ch, ch_next, prime)
1919
ch = ch_next
2020
}
2121
}

bench/algorithm/edigits/1.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn main() {
2828
} else {
2929
mut line := s[i..n]
3030
for _ in 0 .. (10 - n % 10) {
31-
line = '$line '
31+
line = '${line} '
3232
}
33-
print('$line\t:$n')
33+
print('${line}\t:${n}')
3434
}
3535
}
3636
}

bench/algorithm/fannkuch-redux/1.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111

1212
sum, flips := fannkuchredux(n)
1313

14-
println('$sum\nPfannkuchen($n) = $flips')
14+
println('${sum}\nPfannkuchen(${n}) = ${flips}')
1515
}
1616

1717
[direct_array_access]

bench/algorithm/fasta/1.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
}
5656

5757
fn make_repeat_fasta(id string, desc string, src []byte, n int) {
58-
println('>$id $desc')
58+
println('>${id} ${desc}')
5959
mut char_print_idx := 0
6060
mut sb := strings.new_builder(line_width)
6161
unsafe {
@@ -83,7 +83,7 @@ fn make_repeat_fasta(id string, desc string, src []byte, n int) {
8383
}
8484

8585
fn make_random_fasta(mut rand_gen RandGen, id string, desc string, mut table map[byte]f64, n int) {
86-
println('>$id $desc')
86+
println('>${id} ${desc}')
8787
mut prob := 0.0
8888
for k, p in table {
8989
prob += p

bench/algorithm/helloworld/1.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import os
22

33
fn main() {
44
name := if os.args.len > 1 { os.args[1] } else { '' }
5-
println('Hello world $name!')
5+
println('Hello world ${name}!')
66
}

bench/algorithm/http-server/1.v

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ fn main() {
1313
n = strconv.atoi(os.args[1]) or { 10 }
1414
}
1515
port := int(rand.u32_in_range(20000, 50000) or { 23333 })
16-
go vweb.run(&App{}, port)
17-
url := 'http://localhost:$port/api'
16+
spawn vweb.run(&App{}, port)
17+
url := 'http://localhost:${port}/api'
1818
mut ch := chan int{cap: n}
1919
for i in 1 .. (n + 1) {
20-
go send(url, i, ch)
20+
spawn send(url, i, ch)
2121
}
2222
mut sum := 0
2323
for _ in 0 .. n {
@@ -45,7 +45,7 @@ pub fn (mut app App) api() vweb.Result {
4545
value: 0
4646
}
4747
}
48-
return app.text('$data.value')
48+
return app.text('${data.value}')
4949
}
5050

5151
struct Payload {

bench/algorithm/json-serde/1.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ fn main() {
1111
if os.args.len > 2 {
1212
n = strconv.atoi(os.args[2]) or { 3 }
1313
}
14-
json_str := os.read_file('${file_name}.json') ?
15-
data := json.decode(GeoData, json_str) ?
14+
json_str := os.read_file('${file_name}.json')!
15+
data := json.decode(GeoData, json_str)!
1616
println(md5.hexhash(json.encode(data)))
17-
array := []GeoData{len: n, cap: n, init: json.decode(GeoData, json_str) ?}
17+
array := []GeoData{len: n, cap: n, init: json.decode(GeoData, json_str)!}
1818
println(md5.hexhash(json.encode(array)))
1919
}
2020

bench/algorithm/lru/1.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ fn main() {
8080
}
8181
}
8282

83-
println('$hit\n$missed')
83+
println('${hit}\n${missed}')
8484
}

bench/algorithm/lru/2.v

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ fn (mut lcg LCG) next() u32 {
1919
return lcg.seed
2020
}
2121

22-
struct LinkedListNode<T> {
22+
struct LinkedListNode[T] {
2323
mut:
24-
prev &LinkedListNode<T>
25-
next &LinkedListNode<T>
24+
prev &LinkedListNode[T]
25+
next &LinkedListNode[T]
2626
data T
2727
}
2828

29-
struct LinkedList<T> {
29+
struct LinkedList[T] {
3030
mut:
3131
len int
32-
head &LinkedListNode<T> = 0
33-
tail &LinkedListNode<T> = 0
32+
head &LinkedListNode[T] = 0
33+
tail &LinkedListNode[T] = 0
3434
}
3535

36-
fn (mut ll LinkedList<T>) add<T>(data T) &LinkedListNode<T> {
36+
fn (mut ll LinkedList[T]) add[T](data T) &LinkedListNode[T] {
3737
node = &LinkedListNode{data}
3838
ll.__add_node(node)
3939
ll.len += 1
4040
return node
4141
}
4242

43-
fn (mut ll LinkedList<T>) __add_node<T>(node &LinkedListNode<T>) {
43+
fn (mut ll LinkedList[T]) __add_node[T](node &LinkedListNode[T]) {
4444
if ll.head == 0 {
4545
ll.head = node
4646
node.prev = 0
@@ -52,7 +52,7 @@ fn (mut ll LinkedList<T>) __add_node<T>(node &LinkedListNode<T>) {
5252
node.next = 0
5353
}
5454

55-
fn (mut ll LinkedList<T>) __remove<T>(node &LinkedListNode<T>) {
55+
fn (mut ll LinkedList[T]) __remove[T](node &LinkedListNode[T]) {
5656
if ll.head == node {
5757
ll.head = node.next
5858
}
@@ -67,12 +67,12 @@ fn (mut ll LinkedList<T>) __remove<T>(node &LinkedListNode<T>) {
6767
}
6868
}
6969

70-
fn (mut ll LinkedList<T>) move_to_end(node &LinkedListNode<T>) {
70+
fn (mut ll LinkedList[T]) move_to_end(node &LinkedListNode[T]) {
7171
ll.__remove(node)
7272
ll.__add_node(node)
7373
}
7474

75-
fn (mut ll LinkedList<T>) pop_head() &LinkedListNode<T> {
75+
fn (mut ll LinkedList[T]) pop_head() &LinkedListNode[T] {
7676
if self.head != 0 {
7777
head := self.head
7878
self.head = head.next
@@ -82,16 +82,16 @@ fn (mut ll LinkedList<T>) pop_head() &LinkedListNode<T> {
8282
return 0
8383
}
8484

85-
struct Pair<K, V> {
85+
struct Pair[K, V] {
8686
key K
8787
value V
8888
}
8989

90-
struct LRU<K, V> {
90+
struct LRU[K, V] {
9191
size int
9292
mut:
93-
_key_lookup map[K]&LinkedListNode<Pair<K, V>> = {}
94-
_entries LinkedList<Pair<K, V>> = {}
93+
_key_lookup map[K]&LinkedListNode[Pair[K, V]] = {}
94+
_entries LinkedList[Pair[K, V]] = {}
9595
}
9696

9797
fn (mut lru LRU) get(key u32) ?u32 {
@@ -130,7 +130,7 @@ fn main() {
130130
mut rng1 := LCG{
131131
seed: 1
132132
}
133-
mut lru := LRU<u32, u32>{
133+
mut lru := LRU[u32, u32]{
134134
size: size
135135
}
136136

@@ -148,5 +148,5 @@ fn main() {
148148
}
149149
}
150150

151-
println('$hit\n$missed')
151+
println('${hit}\n${missed}')
152152
}

bench/algorithm/mandelbrot/1.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
for i in 0 .. n {
1818
xloc[i / 8][i % 8] = f64(i) * inv - 1.5
1919
}
20-
println('P4\n$n $n')
20+
println('P4\n${n} ${n}')
2121

2222
mut rows := []byte{len: n * chunk_size, cap: n * chunk_size, init: 0}
2323
for chunk_id := 0; chunk_id < n; chunk_id++ {

bench/algorithm/pidigits/1.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() {
4141
digits_printed++
4242
digits_printed_mod_ten := digits_printed % 10
4343
if digits_printed_mod_ten == 0 {
44-
sb.write_string('\t:$digits_printed')
44+
sb.write_string('\t:${digits_printed}')
4545
println(sb.str())
4646
sb.go_back_to(0)
4747
}
@@ -51,7 +51,7 @@ fn main() {
5151
for _ in 0 .. (10 - digits_printed_mod_ten) {
5252
sb.write_byte(` `)
5353
}
54-
sb.write_string('\t:$digits_printed')
54+
sb.write_string('\t:${digits_printed}')
5555
println(sb.str())
5656
}
5757
return

bench/algorithm/pidigits/2.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() {
4747
digits_printed++
4848
digits_printed_mod_ten := digits_printed % 10
4949
if digits_printed_mod_ten == 0 {
50-
sb.write_string('\t:$digits_printed')
50+
sb.write_string('\t:${digits_printed}')
5151
println(sb.str())
5252
sb.go_back_to(0)
5353
}
@@ -57,7 +57,7 @@ fn main() {
5757
for _ in 0 .. (10 - digits_printed_mod_ten) {
5858
sb.write_byte(` `)
5959
}
60-
sb.write_string('\t:$digits_printed')
60+
sb.write_string('\t:${digits_printed}')
6161
println(sb.str())
6262
}
6363
return

bench/algorithm/regex-redux/1.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
'agggta[cgt]a|t[acg]taccct',
2222
'agggtaa[cgt]|[acg]ttaccct',
2323
] {
24-
println('$p ${var_find(content, p)?}')
24+
println('${p} ${var_find(content, p)?}')
2525
}
2626
for p, r in {
2727
'tHa[Nt]': '<4>'
@@ -33,7 +33,7 @@ fn main() {
3333
mut re := regex.regex_opt(p)?
3434
content = re.replace(content, r)
3535
}
36-
println('\n$ilen\n$clen\n$content.len')
36+
println('\n${ilen}\n${clen}\n${content.len}')
3737
}
3838

3939
fn var_find(content string, pattern string) ?int {
@@ -44,6 +44,6 @@ fn var_find(content string, pattern string) ?int {
4444

4545
fn normalize_pattern(pattern string) string {
4646
return pattern.split('|').map(fn (s string) string {
47-
return '($s)'
47+
return '(${s})'
4848
}).join('|')
4949
}

0 commit comments

Comments
 (0)