Lists
Lists are dynamic-sized arrays.
Reading Lists
Use literals to create lists, length to count them, and get when an index may be absent.
scores: List[Int] = [82, 91, 77]
count: Int = scores.length() -- 3
match scores.get(0):
Some(score): print(score) -- 82
None: print("empty")
Building Lists
append returns a list with one more element. Rebind a var when accumulating values.
func main(args: List[String]) -> Void:
var scores: List[Int] = []
scores = scores.append(82)
scores = scores.append(91)
Chaining Transformations
Use method-style list combinators when the work is a data pipeline.
scores = [1000, 77, 50, 85, 11, 200, 400, 12]
func main(args: List[String]):
lowest_passing_scores = scores
.filter(func(score): score >= 70)
.sort()
.take(3)
.map(func(score): score.to_string())
print(lowest_passing_scores.join(", ")) -- prints: 77, 85, 200
Parallel
Use .parallel when each element can be processed independently. It is especially useful for large lists with expensive per-item work; small lists may not repay the scheduling overhead.
results = scores.parallel(func(chunk):
chunk
.filter(func(score): score >= 70)
.map(func(score): expensive_score(score))
)
Filtering Example
Build a new list with explicit iteration when the control flow matters.
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}") -- prints: first passing: 82
None: print("no passing scores")
Try it
terminal
blorp run passing-scores.brp
blorp run score-report.brp