Method Syntax
Method-style calls let ordinary functions read like a pipeline.
First Argument
The first argument of a function can make calls via "method syntax". x.f(y) and f(x, y) are equivalent.
func string_and_reverse(n: Int) -> String:
n
.to_string()
.reverse()
func main(args: List[String]) -> Void:
text: String = string_and_reverse(42) -- "24"
same: String = 42.string_and_reverse() -- "24"
Chains
Method syntax allows left to right operations and keeps nested calls from growing inward.
pure func slug(name: String) -> String:
name
.trim()
.split(" ")
.join("-")
Globally Available
Prelude types such as List, Int, String, Bool, Option, and Result have method-style calls available without imports. The bare function name still needs an import; the method call does not.
pure func globally_available_methods() -> String:
scores: List[Int] = [3, -1, 4]
cleaned: List[Int] = scores
.filter(pure func(n): n > 0)
.map(pure func(n): n.abs())
score_text: String = cleaned
.map(pure func(n): n.to_string())
.join(", ")
name: String = " alice smith "
.trim()
.split(" ")
.map(pure func(part): part.capitalize())
.join(" ")
has_three: Bool = cleaned.contains(3)
flag_text: String = has_three.to_string()
missing: String = cleaned
.get(10)
.map(pure func(n): n.to_string())
.get_or("none")
parsed: Result[Int, String] = Ok(42)
result_text: String = parsed
.get_or(0)
.to_string()
name + ": " + score_text + " / " + flag_text + " / " + missing + " / " + result_text
Example
Non-method syntax requires explicit imports and can be harder to read.
pipeline.brp
import:
list: filter, join, map
func main(args: List[String]) -> Void:
values: List[Int] = [3, -1, 4]
chained: String = values
.filter(func(x): x > 0)
.map(func(x): x * 2)
.map(func(x): to_string(x))
.join(", ")
nested: String = join(
map(
map(
filter(
values,
func(x): x > 0,
),
func(x): x * 2,
),
func(x): to_string(x),
),
", ",
)
print(chained) -- prints: 6, 8
print(nested) -- prints: 6, 8
Try it
terminal
blorp run pipeline.brp