小编Wil*_*ter的帖子

如何将任意函数的HList应用于任意值?

我希望能够将Function1[I, ?]s 的任意列表应用于任意输入I。这是我到目前为止的内容:

    type StringInputFunction[T] = Function[String, T]

    val strLen: String => Int = _.length
    val strRev: String => String = _.reverse

    val functions = strLen :: strRev :: HNil
    val expected = 4 :: "evif" :: HNil

    object applyTo5 extends (StringInputFunction ~> Id) {
      override def apply[T](f: StringInputFunction[T]): Id[T] = f("five")
    }
    def applyFunctionsTo5[FH <: HList, OH <: HList](fs: FH)
        (implicit constrain: UnaryTCConstraint[FH, StringInputFunction],
         mapper: Mapper.Aux[applyTo5.type, FH, OH]): mapper.Out = {
      fs.map(applyTo5)
    }
    applyFunctionsTo5(functions) shouldBe …
Run Code Online (Sandbox Code Playgroud)

scala shapeless

7
推荐指数
1
解决办法
47
查看次数

如何使用Akka TestKit响应失败的Ask模式?

我有一个Akka演员,该演员使用Ask模式从子演员中检索Future,并根据成功和失败采取行动。我无法弄清楚如何模仿儿童演员并以失败作为回应。

这是代码:

import java.util.concurrent.TimeUnit
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.ask
import akka.testkit.{ImplicitSender, TestKitBase, TestProbe}
import akka.util.Timeout
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import scala.util.{Failure, Success}

class Parent(child: ActorRef) extends Actor {
  implicit val timeout = Timeout(5, TimeUnit.SECONDS)
  import context.dispatcher

  override def receive: Receive = {
    case "go" => {
      val senderRef = sender()
      (child ? "question").mapTo[String] onComplete {
        case Success("answer") =>
          senderRef ! "child responded with a successful answer"
        case Failure(throwable) =>
          senderRef ! "child responded with a failure"
      }
    } …
Run Code Online (Sandbox Code Playgroud)

scala akka akka-testkit

5
推荐指数
1
解决办法
1822
查看次数

标签 统计

scala ×2

akka ×1

akka-testkit ×1

shapeless ×1