mers/examples/07_Threads.mers

34 lines
720 B
Plaintext
Raw Normal View History

2023-11-13 14:15:00 +01:00
is_prime := n -> {
0.1.sleep // wow, what an inefficient algorithm, oh no
d := 1
loop {
2023-11-13 14:15:00 +01:00
&d = (d, 1).sum
if d.lt(n) {
if n.modulo(d).eq(0) (false)
} else (true)
}
2023-11-13 14:15:00 +01:00
}
find_primes := start_at -> {
() -> (loop {
2023-11-13 14:15:00 +01:00
out := if start_at.is_prime (start_at)
&start_at = (start_at, 1).sum
out
})
2023-11-13 14:15:00 +01:00
}
primes_count := 0
background_thread := {() ->
2.find_primes.take(20).map(p -> { &primes_count = (primes_count, 1).sum, p }).as_list
}.thread
// Show status to the user while the background thread works
loop {
2023-11-13 14:15:00 +01:00
0.2.sleep
(" Found ", primes_count, " primes...\r").concat.print
if background_thread.thread_finished (())
}
2023-11-13 14:15:00 +01:00
("\nPrimes: ", background_thread.thread_await).concat.println