examples: fix `v build-examples`

pull/5965/head
Delyan Angelov 2020-07-24 13:29:47 +03:00
parent 5acc437367
commit bfca55b87a
8 changed files with 38 additions and 39 deletions

View File

@ -2,7 +2,7 @@ import sync
import time
// Simulate expensive computing using sleep function
fn expensive_computing(id, duration int, wg &sync.WaitGroup) {
fn expensive_computing(id, duration int, mut wg sync.WaitGroup) {
println('Executing expensive computing task (${id})...')
time.sleep_ms(duration)
println('Finish task ${id} on ${duration} ms')
@ -10,11 +10,11 @@ fn expensive_computing(id, duration int, wg &sync.WaitGroup) {
}
fn main() {
wg := sync.new_waitgroup()
mut wg := sync.new_waitgroup()
wg.add(3)
go expensive_computing(1, 100, wg)
go expensive_computing(2, 500, wg)
go expensive_computing(3, 1000, wg)
go expensive_computing(1, 100, mut wg)
go expensive_computing(2, 500, mut wg)
go expensive_computing(3, 1000, mut wg)
// Join all tasks
wg.wait()
println('All jobs finished!')

View File

@ -2,7 +2,7 @@ import net.http
import sync
import time
fn vlang_time(wg &sync.WaitGroup) ?string {
fn vlang_time(mut wg sync.WaitGroup) ?string {
start := time.ticks()
data := http.get('https://vlang.io/utc_now')?
finish := time.ticks()
@ -12,7 +12,7 @@ fn vlang_time(wg &sync.WaitGroup) ?string {
return data.text
}
fn remote_ip(wg &sync.WaitGroup) ?string {
fn remote_ip(mut wg sync.WaitGroup) ?string {
start := time.ticks()
data := http.get('https://api.ipify.org')?
finish := time.ticks()
@ -23,10 +23,10 @@ fn remote_ip(wg &sync.WaitGroup) ?string {
}
fn main() {
wg := sync.new_waitgroup()
mut wg := sync.new_waitgroup()
wg.add(2)
// Run tasks async
go vlang_time(wg)
go remote_ip(wg)
go vlang_time(mut wg)
go remote_ip(mut wg)
wg.wait()
}

View File

@ -100,33 +100,30 @@ fn energy(sys System) f64 {
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},
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},
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() {
sys := &System {arr_momentum(), arr_position()}
offsetmomentum(mut sys)
println('${energy(sys):.9f}') //-0.169075164
for _ in 0..50000000 {
advance(mut sys, 0.01)
}
println('${energy(sys):.9f}') //-0.169059907
mut sys := &System {arr_momentum(), arr_position()}
offsetmomentum(mut sys)
println('${energy(sys):.9f}') //-0.169075164
for _ in 0..50_000_000 {
advance(mut sys, 0.01)
}
println('${energy(sys):.9f}') //-0.169059907
}

View File

@ -46,5 +46,6 @@ fn callback(req picohttpparser.Request, mut res picohttpparser.Response) {
}
fn main() {
println('Starting webserver on http://127.0.0.1:8088/ ...')
picoev.new(8088, &callback).serve()
}

View File

@ -128,7 +128,7 @@ const (
black = C.sfons_rgba(0, 0, 0, 255)
)
fn (state &AppState) render_font() {
fn (mut state AppState) render_font() {
lh := 30
mut dy := lh
if !state.inited {

View File

@ -137,7 +137,7 @@ struct Game {
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
[if showfps]
fn (game &Game) showfps() {
fn (mut game Game) showfps() {
game.frame++
last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0
ticks := f64(game.second_sw.elapsed().microseconds())/1000.0
@ -149,7 +149,7 @@ fn (game &Game) showfps() {
}
}
fn frame(game &Game) {
fn frame(mut game Game) {
game.frame_sw.restart()
game.gg.begin()
game.draw_scene()
@ -301,7 +301,7 @@ fn (mut g Game) get_tetro() {
}
// TODO mut
fn (g &Game) drop_tetro() {
fn (mut g Game) drop_tetro() {
for i in 0..tetro_size{
tetro := g.tetro[i]
x := tetro.x + g.pos_x

View File

@ -165,13 +165,14 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
for {
pret := req.parse_request(s, 100)
if pret <= 0 && s.len > 0 {
C.memmove(buf, s.str, s.len)
unsafe { C.memmove(buf, s.str, s.len) }
p.idx[fd] = s.len
p.oidx[fd] = int(res.buf) - int(res.buf_start)
break
}
if req.method.str[0]==`p` || req.method.str[0]==`P` || req.method.str[0]==`d` || req.method.str[0]==`D` {
mut j := 0
c0 := unsafe { req.method.str[0] }
if c0 ==`p` || c0 == `P` || c0 == `d` || c0 == `D` {
mut j := 0
for {
if j == req.num_headers {
break
@ -238,7 +239,7 @@ pub fn new(port int, cb voidptr) &Picoev {
C.picoev_init(max_fds)
loop := C.picoev_create_loop(max_timeout)
pv := &Picoev{
mut pv := &Picoev{
loop: loop
cb: cb
date: C.get_date()

View File

@ -9,7 +9,7 @@ pub mut:
buf byteptr
}
[inline] [unsafe_fn]
[inline]
fn (mut r Response) write_str(s string) {
unsafe {
C.memcpy(r.buf, s.str, s.len)