Skip to main content
Version: 0.0.6

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 the int type (integer)
  • 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 :
info

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
[WARN]	script.vp:5:23 `n` might be missing
✓ files successfully transpiled!
[1] 0 1 1 2 3 5 8 13 21 34

Running the command prints the sequence, along with a WARN: the n variable (the function argument) might be missing since it has no default value.

Vapour only warns about this, but running the code below will fail in R because n is missing.

fibonacci()

We can easily fix that issue, either add a check for missing or give the function a default value.

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
}