Tasks
Scoring
Run independent scoring and preparation functions at the same time.
Source
tasks/scoring.vibe
# vibe: 0.4
def score_user(user)
user[:score] * user[:weight]
end
def score_users(users)
Tasks.map(users, max: 2, with: :score_user)
end
def summarize_users(users)
scores = score_users(users)
{
scores: scores,
total: scores.sum
}
end
def prepare_user(user)
"prepared:" + user[:id]
end
def prepare_pair(first, second)
Tasks.run(max: 2) do |tasks|
left = tasks.spawn(:prepare_user, first)
right = tasks.spawn(:prepare_user, second)
tasks.wait
left_result = left.value
right_result = right.value
return [left_result, right_result]
end
end
def run
users = [
{ id: "u1", score: 10, weight: 2 },
{ id: "u2", score: 7, weight: 3 }
]
{
summary: summarize_users(users),
prepared: prepare_pair(users[0], users[1])
}
end
Output
Press Run example to run this code.