Vibescript Showcase

Concurrent scoring

Score many records at the same time with Tasks.map.

Showcase
Source showcase/concurrency/concurrent_scoring.vibe
# title: Concurrent scoring
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Score many records at the same time with Tasks.map.
# description: Tasks.map calls the same function for each record. max: limits active tasks, and results stay in input order.
# tags: concurrency, tasks, structured-concurrency, fanout
# featured: true
# feature_rank: 0
# vibe: 0.4

def score_user(user)
  user[:score] * user[:weight]
end

def score_users(users)
  Tasks.map(users, max: 4, with: :score_user)
end

def run
  users = [
    { id: "u1", score: 10, weight: 3 },
    { id: "u2", score: 7, weight: 5 },
    { id: "u3", score: 9, weight: 2 },
    { id: "u4", score: 4, weight: 8 }
  ]

  scores = score_users(users)

  {
    scores: scores,
    total: scores.sum
  }
end
Output
Press Run example to run this code.