Lists
Lists are COW-friendly dynamic arrays: updates return new list values.
Literals
Use [] for an empty list and [1, 2, 3] for a literal.
list-literal.brp
scores: List[Int] = [82, 91, 77]Append
append returns a list with one more element.
append.brp
var scores: List[Int] = []
scores = scores.append(91)Get
get returns Option[T] so out-of-bounds access is explicit.
get.brp
match scores.get(0):
Some(score): print(score.to_string())
None: print("empty")Length
length returns the number of elements.
length.brp
count: Int = scores.length()Iteration
for item in items iterates over the list.
iteration.brp
for score in scores:
print(score.to_string())COW-Friendly Rebinding
The common update shape is var xs followed by xs = xs.append(value).
cow-rebind.brp
var items: List[String] = []
items = items.append("first")Example
passing-scores.brp
pure func passing(scores: List[Int]) -> List[Int]:
var result: List[Int] = []
for score in scores:
if score >= 70:
result = result.append(score)
result
func main(args: List[String]) -> Void:
scores: List[Int] = [82, 61, 91]
good: List[Int] = passing(scores)
match good.get(0):
Some(score): print("first passing: " + score.to_string())
None: print("no passing scores")
Try It
terminal
blorp run passing-scores.brp