elm
elm

Reputation: 20405

Scala extract value from Future[Seq[T]] into Future[T]

For a given Seq wrapped in a Future, namely for instance

import scala.concurrent._
import ExecutionContext.Implicits.global

val xs = Future { Seq(1,2,3) }

how to extract the first (one only) value in the collection into another Future, namely

Future { 1 }

Upvotes: 0

Views: 284

Answers (1)

Łukasz
Łukasz

Reputation: 8663

val xs = Future { Seq(1,2,3) }
val x = xs.map(_.head)

x will be another Future, and xs is still usable, so you can do anything with it.

Upvotes: 6

Related Questions