elm elm - 1 year ago 139
Scala Question

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

For a given

wrapped in a
, 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
, namely

Future { 1 }

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download