部分应用的函数是否可以调用其部分应用的自身?
Is it possible for a partially applied function to call its partially applied self?
我已经开始使用 Akka 并发现我的大多数 actor 都具有部分不可变状态和部分可变状态。两者都可以合并到 State
案例 class 中,然后可以仅复制其可变状态并传回 apply
以更新 Behavior
。
但是,如果没有必要,那将是神奇的。部分应用的 Scala 函数是否有可能以某种方式递归调用自身,但从其第二个参数列表开始?而不是从头开始整个链条?
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
)(
mutableState: List[String] = Nil
): Behavior[Command] = Behaviors.receiveMessage {
// without respecifying all immutable state:
case AddA() => CallIts2ndParamList("A" :: mutableState)
// what I'm trying to avoid:
case AddB() => apply(
immutableState1,
immutableState2,
immutableState3
)("B" :: mutableState)
}
啊,也许我在错误的领域寻找解决方案。嵌套函数实际上应该可以解决问题![=11=]
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
): Behavior[Command] = {
def nestedApply(mutableState: List[String]): Behavior[Command] =
Behaviors.receiveMessage {
case AddA() => nestedApply("A" :: mutableState)
}
nestedApply(Nil)
}
我已经开始使用 Akka 并发现我的大多数 actor 都具有部分不可变状态和部分可变状态。两者都可以合并到 State
案例 class 中,然后可以仅复制其可变状态并传回 apply
以更新 Behavior
。
但是,如果没有必要,那将是神奇的。部分应用的 Scala 函数是否有可能以某种方式递归调用自身,但从其第二个参数列表开始?而不是从头开始整个链条?
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
)(
mutableState: List[String] = Nil
): Behavior[Command] = Behaviors.receiveMessage {
// without respecifying all immutable state:
case AddA() => CallIts2ndParamList("A" :: mutableState)
// what I'm trying to avoid:
case AddB() => apply(
immutableState1,
immutableState2,
immutableState3
)("B" :: mutableState)
}
啊,也许我在错误的领域寻找解决方案。嵌套函数实际上应该可以解决问题![=11=]
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
): Behavior[Command] = {
def nestedApply(mutableState: List[String]): Behavior[Command] =
Behaviors.receiveMessage {
case AddA() => nestedApply("A" :: mutableState)
}
nestedApply(Nil)
}