ruby-concurrency / ruby-concurrency/concurrent-ruby
Support Hash of Futures for Promises.zip
Nessuno ha ancora preso questa issue.
- Lingua principale
- Ruby
- Stelle
- 5.8k
- Fork
- 420
- Merge medio
- 20h 45m
- PR unite (30g)
- 4
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- ruby
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100