How to I perform a reduce/fold operation on the Seq and then get the final value.

I'm performing an operation (in this case a Redis call) that returns a Future. I'm processing the Future (results) using a map operation.

The map operation returns a Future[Seq[Any]] type.

res0: scala.concurrent.Future[Seq[Any]] = scala.concurrent.impl.Promise$DefaultPromise@269f8f79

Now I want to perform some operations(fold/reduce) on this Seq and then get a final value. How can I achieve this?

  implicit val akkaSystem = akka.actor.ActorSystem()
  val redisClient = RedisClient()
  val sentimentZSetKey = "dummyzset"
  val currentTimeStamp = System.currentTimeMillis()
  val end = Limit(currentTimeStamp)
  val start = Limit(currentTimeStamp - 60 * 100000)
  val results = redisClient.zrangebyscoreWithscores(ZSetKey, start, end)
  implicit val formats = DefaultFormats
  import org.json4s._
  import org.json4s.native.JsonMethods._
  import org.json4s.DefaultFormats


  results.map {
    seq => seq.map {
      element => element match {

        case (byteString, value) => {
          val p = byteString.decodeString("UTF-8")
          try {
            val ph = parse(p).extract[MyClass]
            ph

          } catch {
            case e: Exception => println(e.getMessage)
          }
        }
        case _ =>
      }
    }
  } 
有帮助吗?

解决方案

Blocking is discouraged when using futures in Scala, but it can be done with the Await function as per the link. Since you want to further transform the sequence, you are better off using functional composition as in these examples.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top