Control flow
This chapter covers the everyday building blocks: ranges, indexing, operators, conditions, and loops. Most of it looks like R, with a few deliberate differences.
Ranges
In Vapour the colon introduces types, so ranges use .. instead of :.
- Vapour
- R
let x: int = 1..10
x = 1:10
Both ends of a range must be numbers, "a"..3 is an error.
Indexing
[, [[, and $ work as in R.
let x: int = (10, 20, 30)
let first: int = x[1]
x[2] = 25
$ is only for custom types with attributes,
using it on a vector is an error.
let x: int = (10, 20, 30)
# error: cannot use `$` on type `int`
x$total = 60
Operators
Operators are the same as in R.
| Kind | Operators |
|---|---|
| Arithmetic | + - * / ^ %% |
| Comparison | == != < > <= >= |
| Logical | && || ! |
| Infix | %in%, or any %name% |
let x: int = 10
let in_range: bool = x > 1 && x <= 20
let found: bool = x %in% (5, 10)
Comparisons and logical operators return bool.
Comparing values of different types is reported, since it's usually
a mistake.
let x: int = 10
# info: comparison `char` > `int` is not logical
let odd: bool = "a" > x
Pipe
The native pipe |> works as in R.
let total: num = (1, 2, 3) |> sum()
Conditions
if, else if, and else work as in R. The braces are required.
- Vapour
- R
let x: int = 2
if(x == 1) {
print("one")
} else if(x == 2) {
print("two")
} else {
print("other")
}
x = 2
if(x == 1) {
print("one")
} else if(x == 2) {
print("two")
} else {
print("other")
}
if can also produce a value.
let score: int = 72
let grade: char = if(score >= 50) { "pass" } else { "fail" }
for loops
The loop variable is declared with let and a type, like any other variable.
- Vapour
- R
for(let i: int in 1..3) {
print(i)
}
for(i in 1:3) {
print(i)
}
Vapour checks two things:
-
The type of the loop variable matches the elements.
let ids: int = (1, 2, 3)
# error: `id` expects `char`, elements are `int`
for(let id: char in ids) {
print(id)
} -
The value can be looped over. Vectors and list types can be, other custom types like
objectandstructcannot.
while loops
- Vapour
- R
let i: int = 0
while(i < 3) {
i += 1
}
i = 0
while(i < 3) {
i = i + 1
}
repeat, break, and next
repeat loops until it reaches a break. next skips to the next
iteration. Both work in any loop.
let count: int = 0
repeat {
count += 1
if(count == 2) {
next
}
if(count >= 5) {
break
}
}
Using break or next outside of a loop is an error.
This includes the body of a function called from a loop:
a function body starts outside of any loop.
# error: `break` used outside of a loop
break
Calling package functions
Call functions from other packages with ::, or ::: for unexported ones.
let title: char = tools::toTitleCase("hello world")
Vapour checks that the package is installed and exports the function, and gives you a hint if not.
# hint: package `notapkg` is not installed
notapkg::run()
# hint: `tools::notAFunction` not found
tools::notAFunction()
Prefer pkg::fn() over library(pkg): it says where each function
comes from, and it's what lets Vapour check the call.
Vapour hints when it sees library() or require().
Recap
- Ranges use
..:1..10. [,[[, operators, and|>work as in R,$is for custom types.- Braces are required on
if,for,while, andrepeat. fordeclares its variable:for(let i: int in 1..10).- Call package functions with
pkg::fn().
The precise rules are in the spec: operators, control flow, and operator types.