I would like to check if a Future[Seq[String]] is Empty and I am using
if(!Future.sequence(sortSeq).isEmpty){
//doSomething
}
but it throws me error ?
I would like to check if a Future[Seq[String]] is Empty and I am using
if(!Future.sequence(sortSeq).isEmpty){
//doSomething
}
but it throws me error ?
I suppose the type of sortSeq
is Future[Seq[String]]
, so you don't need Future.sequence
.
You have to wait for result of your Future
and then check if result is empty:
import scala.concurrent.duration._
if(!Await.result(sortSeq.map{_.isEmpty}, 5.seconds){
//doSomething
}
If you can execute doSomething
in a different thread you could try this:
val someRusultFuture = sortSeq.map{ rs =>
if(!rs.isEmpty){
//doSomething
}
}
but you'll have to wait for result of someRusultFuture
.
Proof it works:
scala> import concurrent.{Future, Await}
import concurrent.{Future, Await}
scala> import scala.concurrent.duration._
import scala.concurrent.duration._
scala> val sortSeq = Future{ Thread.sleep(10000); Seq("a") }
sortSeq: scala.concurrent.Future[Seq[String]] = scala.concurrent.impl.Promise$DefaultPromise@3592f7c6
scala> Await.result(sortSeq.map{_.isEmpty}, 11.seconds)
res1: Boolean = false
Future.sequence
method is used to transform a TraversableOnce[Future[A]]
into a Future[TraversableOnce[A]]
.
I think you can forget whether the Seq
is empty or not and just work with the Seq
using the map
function of Future
val s = Future[Seq[String]](Seq("s", "e", "q"))
val p = s.map(s => s.foreach(println))
This works because, the empty check is performed implicitly in the background. In the below example, when the Seq
is empty, nothing will be printed.
scala> val s = Future[Seq[String]](Seq.empty)
s: scala.concurrent.Future[Seq[String]] = Future(<not completed>)
scala> val p = s.map(s => s.foreach(println))
p: scala.concurrent.Future[Unit] = Future(<not completed>)
If you really want to perform an empty check, you can also use withFilter
.
With a non empty Seq
scala> val s = Future[Seq[String]](Seq("s", "e", "q"))
s: scala.concurrent.Future[Seq[String]] = Future(Success(List(s, e, q)))
scala> val p = s.withFilter(_.nonEmpty).map(s => s.foreach(println))
p: scala.concurrent.Future[Unit] = Future(<not completed>)
s
e
q
With an empty Seq
scala> val s = Future[Seq[String]](Seq.empty)
s: scala.concurrent.Future[Seq[String]] = Future(Success(List()))
scala> val p = s.withFilter(_.nonEmpty).map(s => s.foreach(println))
p: scala.concurrent.Future[Unit] = Future(<not completed>)
Also you can do the empty check as mentioned in the above answer
val someRusultFuture = sortSeq.map{ rs =>
if(!rs.isEmpty){
//doSomething
}
}
Hope this helps.