문제

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