From a6b78944c9d9a4df431375beb0fb051d1c597ae9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 21 Sep 2021 00:14:50 +0800 Subject: [PATCH] Add example to README. --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index e4746f9f..3f2ac980 100644 --- a/README.md +++ b/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). +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 ------------