i have results: list[future[option[t]]]
contains (parallel) calculations.
i want obtain first non-none
result possible, or return none
if calculations return none
.
currently, i'm doing (which consider ugly) handle case future.find
doesn't find results.
future.find(results)(r => r.isdefined) map { case some(hit) => hit case _ => none }
which give me future[option[t]]
(what want).
is there cleaner way obtain future[option[t]]
: i.e. without having manually flatten future[option[option[t]]]
?
rather specialise options, write general function such as:
def collectfirst[a, b](futures: list[future[a]])(pfn: partialfunction[a, b]) (implicit ex: executioncontext): future[option[b]] = future.find(futures)(pfn.isdefinedat) map (_.collect(pfn))
in terms of this, rephrase problem as:
collectfirst(results) { case some(hit) => hit }
Comments
Post a Comment