Pure Functions
pure func marks deterministic code and keeps effects at the boundary.
Pure Func
A pure func cannot perform I/O, call impure functions, mutate global state, or accept impure callbacks.
pure.brp
pure func add_one(n: Int) -> Int:
n + 1Local Mutation
Local var bindings are allowed when the mutation cannot be observed outside the function.
local-var.brp
pure func total(xs: List[Int]) -> Int:
var sum: Int = 0
for x in xs:
sum += x
sumEffect Boundaries
Keep reading, printing, files, and processes in impure functions such as main.
boundary.brp
pure func label(n: Int) -> String:
n.to_string()
func main(args: List[String]) -> Void:
print(label(42))Pure Callbacks
Collection and parallel APIs can accept pure callbacks when they need deterministic work.
parallel-callback.brp
items.parallel(
pure func(chunk): chunk.map(pure func(x): x * 2)
)Example
average.brp
pure func average(scores: List[Int]) -> Int:
var total: Int = 0
for score in scores:
total += score
total / scores.length()
func main(args: List[String]) -> Void:
scores: List[Int] = [82, 91, 77]
print(average(scores).to_string())
Try It
terminal
blorp run average.brp