mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 14:13:52 +01:00
added thread example and made small changes to the readme
This commit is contained in:
parent
c2362aca13
commit
765a2597ee
16
README.md
16
README.md
@ -275,15 +275,15 @@ Mers has the following builtin types:
|
|||||||
+ list types are written as a single type enclosed in square brackets: [string]. TODO! this will likely change to [string ...] or something similar to allow 1-long tuples in function args.
|
+ list types are written as a single type enclosed in square brackets: [string]. TODO! this will likely change to [string ...] or something similar to allow 1-long tuples in function args.
|
||||||
+ list values are created by putting any number of statements in square brackets, prefixing the closing bracket with ...: ["hello" "mers" "world" ...].
|
+ list values are created by putting any number of statements in square brackets, prefixing the closing bracket with ...: ["hello" "mers" "world" ...].
|
||||||
- functions
|
- functions
|
||||||
+ function types are written as fn(args) out_type (TODO! implement this)
|
+ function types are written as `fn(args) out_type`. (TODO! implement this)
|
||||||
+ function values are created using the (<first_arg_name> <first_arg_type> <second_arg_name> <second_arg_type>) <statement> syntax: anonymous_power_function = (a int b int) a.pow(b).
|
+ function values are created using the `(first_arg_name first_arg_type second_arg_name second_arg_type) statement` syntax: `anonymous_power_function = (a int b int) a.pow(b)`.
|
||||||
+ to run anonymous functions, use the run() builtin: anonymous_power_function.run(4 2) evaluates to 16.
|
+ to run anonymous functions, use the run() builtin: `anonymous_power_function.run(4 2)` evaluates to `16`.
|
||||||
+ note: functions are defined using the fn <name>(<args>) <statement> syntax and are different from anonymous functions because they aren't values and can be run directory: fn power_function(a int b int) a.pow(b) => power_function(4 2) => 16
|
+ note: functions are defined using the `fn name(args) statement` syntax and are different from anonymous functions because they aren't values and can be run directly: `fn power_function(a int b int) a.pow(b)` => `power_function(4 2)` => `16`
|
||||||
- thread
|
- thread
|
||||||
+ a special type returned by the thread builtin. It is similar to JavaScript promises and can be awaited to get the value once it has finished computing.
|
+ a special type returned by the thread builtin. It is similar to JavaScript promises and can be awaited to get the value once it has finished computing. Reading the thread example is probably the best way to see how this works.
|
||||||
- reference
|
- reference
|
||||||
+ a mutable reference to a value. &<type> for the type and &<statement> for a reference value.
|
+ a mutable reference to a value. `&type` for the type and `&statement` for a reference value (usually `&varname`).
|
||||||
- enum
|
- enum
|
||||||
+ wraps any other value with a certain identifier.
|
+ wraps any other value with a certain identifier.
|
||||||
+ the type is written as <enum>(<type>): many builtins use Err(String) to report errors.
|
+ the type is written as `enum(type)`: many builtins use `Err(String)` to report errors.
|
||||||
+ to get a value, use <enum>: <statement>.
|
+ to get a value, use `enum: statement`. `Err: "something went wrong"`
|
||||||
|
0
mers/Cargo.lock
generated
Normal file → Executable file
0
mers/Cargo.lock
generated
Normal file → Executable file
0
mers_libs/http_requests_v1/Cargo.lock
generated
Normal file → Executable file
0
mers_libs/http_requests_v1/Cargo.lock
generated
Normal file → Executable file
35
thread.txt
Executable file
35
thread.txt
Executable file
@ -0,0 +1,35 @@
|
|||||||
|
// this is true by default so the example doesn't finish too quickly or too slowly depending on your hardware.
|
||||||
|
// you can set it to false and tweak the max value for a more authentic cpu-heavy workload.
|
||||||
|
fake_delay = true
|
||||||
|
|
||||||
|
// this will be shared between the two threads to report the progress in percent (0-100%).
|
||||||
|
progress = 0
|
||||||
|
|
||||||
|
// an anonymous function that sums all numbers from 0 to max.
|
||||||
|
// it captures the progress variable and uses it to report its status to the main thread, which will periodically print the current progress.
|
||||||
|
// once done, it returns a string with the sum of all numbers.
|
||||||
|
calculator = (max int) {
|
||||||
|
sum = 0
|
||||||
|
for i max {
|
||||||
|
i = i.add(1)
|
||||||
|
sum = sum.add(i)
|
||||||
|
if fake_delay sleep(1)
|
||||||
|
progress = i.mul(100).div(max)
|
||||||
|
}
|
||||||
|
"the sum of all numbers from 0 to {0} is {1}!".format(max sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the thread. if fake_delay is true, calculate 1+2+3+4+5+6+7+8+9+10. if fake_delay is false, count up to some ridiculously large number.
|
||||||
|
slow_calculation_thread = calculator.thread(if fake_delay 10 else 20000000)
|
||||||
|
|
||||||
|
// every second, print the progress. once it reaches 100%, stop
|
||||||
|
while {
|
||||||
|
sleep(1)
|
||||||
|
println("{0}%".format(progress))
|
||||||
|
progress.eq(100) // break from the loop
|
||||||
|
}
|
||||||
|
|
||||||
|
// use await() to get the result from the thread. if the thread is still running, this will block until the thread finishes.
|
||||||
|
result = slow_calculation_thread.await()
|
||||||
|
|
||||||
|
println("Thread finished, result: {0}".format(result))
|
Loading…
Reference in New Issue
Block a user