Basic Example
This is a very basic example of using Vapour in the hope it helps you get started, We'll create a function to create the Fibonacci sequence.
Create a directory and place a script.vp
file in it.
func fibonacci(n: int): int {
let a: int = 0
let b: int = 1
let sequence: int = ()
for(let i:int in 1..n) {
sequence <- c(sequence, a)
let next_num: int = a + b
a = b
b = next_num
}
return sequence
}
- We declare the function with the
func
keyword. - We give parameter
n
theint
type (integer) - We indicate that the function returns a vector of integers.
- We indicate that the function returns a vector of integers.
- We declare
sequence
as an empty vector with()
- We range over with
..
where in R we do it with:
If you typed the above in your editor you will have noticed a warning already.
You can then call the function once it's declared.
# ... function declaration
fibonacci(10)
Once that done we are ready to run the script!
Run the command below from the terminal,
the -run-only
flag has Vapour run the script and not
generate the corresponding R file (we'll do that later).
vapour -infile=script.vp -run-only
[HINT] file vapour.vp, line 6, character 24: `n` might be missing
[INFO] file vapour.vp, line 6, character 10: `i` is never used
Running the command display an INFO
and a HINT
, indeed the i
variable declared in the for
loop is never used but importantly
the n
variable (the function variable) might be missing since it
has no default value.
Running the code below will fail because n
is missing.
fibonacci()
We can easily fix that issue, either add a check for missing
or give the function a default value.
- Default value
- Missing check
func fibonacci(n: int = 5): int {
let a: int = 0
let b: int = 1
let sequence: int = ()
for(let i:int in 1..n) {
sequence <- c(sequence, a)
let next_num: int = a + b
a = b
b = next_num
}
return sequence
}
func fibonacci(n: int): int {
stopifnot(!missing(n))
let a: int = 0
let b: int = 1
let sequence: int = ()
for(let i:int in 1..n) {
sequence <- c(sequence, a)
let next_num: int = a + b
a = b
b = next_num
}
return sequence
}