iul*_*tru 8 functional-programming scala
是否有可能在Scala中获得部分函数的域?
例如:
val f: PartialFunction[Int, Unit] = {
case 1 => println("This is 1")
case 2 => println("This is 2")
}
Run Code Online (Sandbox Code Playgroud)
有没有办法得到这样的东西:
val list = f.getDomain
Run Code Online (Sandbox Code Playgroud)
这表示值1和2?
更新:我正在尝试构建通知系统(事件总线).订阅者将如下所示:
class SomeSubscriber extends Subscriber {
notifications {
case LoginEvent(date) => println("Login on " + date)
case LogoutEvent(date) => println("Logout on " + date)
case e: Notification[Any] => async {
println("Other notification: " + e)
ui {
println("UI in async! " + e)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的NotiticationService(事件调度程序)中,我想访问在每个"通知"块中声明的事件,以便我可以将通知推送给订阅者.我怎样才能做到这一点?
提前致谢.
Dan*_*ton 11
如果您有一个集合,并且您想知道哪些元素也属于该域f,您可以使用filter和isDefinedAt喜欢这样:
scala> 1 to 10 filter f.isDefinedAt
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2)
Run Code Online (Sandbox Code Playgroud)
这只是详尽地检查.我不知道是否有更好的方法.
| 归档时间: |
|
| 查看次数: |
516 次 |
| 最近记录: |