Parallel Collection Work
List.parallel splits pure collection transformations across worker threads.
Pure Callbacks
The callback is pure, which prevents shared mutable state and I/O inside the parallel work.
items.parallel(
pure func(chunk): chunk.map(pure func(x): x * 2)
)
List.parallel()
parallel hands the callback a ParallelList view with parallel-safe map, filter, and filter_map.
items.parallel(
pure func(chunk): chunk.map(pure func(x): x * x)
)
When It Helps
Use it for expensive per-element work, not tiny transforms where scheduling costs dominate.
pure func expensive(n: Int) -> Int:
n * n + n
Collecting Results
parallel returns an ordinary List after the scoped work completes.
results: List[Int] = transform(items)
Collect Concurrent Results
List.concurrent allows impure callbacks and bounds active tasks. It returns List[Result[T, ConcurrencyError]] in input order, not completion order. List.parallel requires pure callbacks and returns ordinary values.
import:
channel: sleep_for
units: milliseconds
func render_page(name: String) -> String:
sleep_for(milliseconds(10))
"rendered ${name}"
func render_all(names: List[String]) -> List[Result[String, ConcurrencyError]]:
names.concurrent(4, func(name: String): render_page(name))
A Shared Deadline
concurrent_with_timeout applies one whole-operation deadline. Inspect every result rather than reporting a partial batch as successful.
import:
channel: sleep_for
units: milliseconds, seconds
func fetch_metric(id: Int) -> Int:
sleep_for(milliseconds(10))
id * 10
func fetch_all(ids: List[Int]) -> List[Result[Int, ConcurrencyError]]:
ids.concurrent_with_timeout(4, seconds(5), func(id: Int):
fetch_metric(id)
)
Example
pure func expensive(n: Int) -> Int:
n * n + n
pure func transform(items: List[Int]) -> List[Int]:
items.parallel(pure func(chunk: ParallelList[Int]):
chunk.map(pure func(n): expensive(n))
)
pure func sum(items: List[Int]) -> Int:
var total: Int = 0
for item in items:
total += item
total
func main(args: List[String]) -> Void:
print(sum(transform([1, 2, 3, 4]))) -- prints: 40
Try it
blorp run parallel-chunks.brp