ruby-concurrency / ruby-concurrency/concurrent-ruby

Support Hash of Futures for Promises.zip

Open
#777 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement looking-for-contributor
Dominant language
Ruby
Stars
5.8k
Forks
420
Avg merge
20h 45m
Merged PRs (30d)
4

Description

Reading the guide for the Promises work in 1.1.x I came across the fantastic Concurrent::Promises.zip method.

It's interface, per the guide, expects an array of Futures and will return a final value of said array's values. This is beneficial, but what would make my (and I suspect many other's) code cleaner is to optionally accept a Hash of promises instead of an Array.

e.g.

map_of_work = {
  multiply: Concurrent::Promises.future { 3*2 },
  divide: Concurrent::Promises.future { 3/2 },
  add: Concurrent::Promises.future { 3 + 2 },
  subtract: Concurrent::Promises.future { 3 - 2 },
}
# => {:multiply=>#<Concurrent::Promises::Future:0x00007fc474fad8a0 pending>,
#     :divide=>#<Concurrent::Promises::Future:0x00007fc474fad008 pending>,
#     :add=>#<Concurrent::Promises::Future:0x00007fc474fa7d60 pending>,
#     :subtract=>#<Concurrent::Promises::Future:0x00007fc474fa6ca8 pending>}
Concurrent::Promises.zip(map_of_work).value!
# => {:multiply=>6,
#     :divide=>1,
#     :add=>5,
#     :subtract=>1}

Why? Because it's a common pattern in Javascript libraries[1][2], and thus my team.

Arrays are slightly more cumbersome where I have to remember that index 2 is always for the addition work. Should another developer prepend more Futures to the array, we must not forget to change all the indexes referenced thereafter. With hashes, an :add is always an :add.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry point is Concurrent::Promises.zip; start by reading its current array-oriented behavior and the linked Promises guide. Support the shown hash-of-promises input while preserving keys and resolving values, then confirm the example returns the expected keyed result.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.