Add example to README.
This commit is contained in:
parent
caf3c7e698
commit
a6b78944c9
41
README.md
41
README.md
@ -71,6 +71,47 @@ For those who actually want their own language
|
|||||||
* Extend the language with [custom syntax](https://rhai.rs/book/engine/custom-syntax.html).
|
* Extend the language with [custom syntax](https://rhai.rs/book/engine/custom-syntax.html).
|
||||||
|
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
|
||||||
|
The [`scripts`](https://github.com/rhaiscript/rhai/tree/master/scripts) subdirectory contains sample Rhai scripts.
|
||||||
|
|
||||||
|
Below is a the standard _Fibonacci_ example for scripting languages:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// This Rhai script calculates the n-th Fibonacci number using a really dumb algorithm
|
||||||
|
// to test the speed of the scripting engine.
|
||||||
|
|
||||||
|
const TARGET = 28;
|
||||||
|
const REPEAT = 5;
|
||||||
|
|
||||||
|
fn fib(n) {
|
||||||
|
if n < 2 {
|
||||||
|
n
|
||||||
|
} else {
|
||||||
|
fib(n-1) + fib(n-2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print(`Running Fibonacci(28) x ${REPEAT} times...`);
|
||||||
|
print("Ready... Go!");
|
||||||
|
|
||||||
|
let result;
|
||||||
|
let now = timestamp();
|
||||||
|
|
||||||
|
for n in range(0, REPEAT) {
|
||||||
|
result = fib(TARGET);
|
||||||
|
}
|
||||||
|
|
||||||
|
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||||
|
|
||||||
|
print(`Fibonacci number #${TARGET} = ${result}`);
|
||||||
|
|
||||||
|
if result != 317_811 {
|
||||||
|
print("The answer is WRONG! Should be 317,811!");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Project Site
|
Project Site
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user