Vibescript Showcase
Batch pipeline
Split records into batches, process them at the same time, and sort the results.
Source
showcase/concurrency/batch_pipeline.vibe
# title: Batch pipeline
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Split records into batches, process them at the same time, and sort the results.
# description: each_slice builds batches, Tasks.map processes each record, and partition separates the results.
# tags: concurrency, tasks, collections, fanout
# vibe: 0.4
def enrich(order)
{
id: order[:id],
total: order[:qty] * order[:price],
priority: order[:qty] >= 5
}
end
def process_batch(batch)
Tasks.map(batch, max: 4, with: :enrich)
end
def run
orders = [
{ id: "o1", qty: 2, price: 10 },
{ id: "o2", qty: 6, price: 4 },
{ id: "o3", qty: 1, price: 25 },
{ id: "o4", qty: 8, price: 3 },
{ id: "o5", qty: 3, price: 7 }
]
enriched = []
orders.each_slice(2) do |batch|
enriched = enriched + process_batch(batch)
end
parts = enriched.partition { |order| order[:priority] }
{
enriched: enriched,
priority_orders: parts[0].map { |order| order[:id] },
standard_orders: parts[1].map { |order| order[:id] },
total_value: enriched.map { |order| order[:total] }.sum
}
end
Output
Press Run example to run this code.