【发布时间】:2013-11-04 09:37:12
【问题描述】:
我目前正在尝试进入 Scala 世界。实际上,我正在尝试实现没有可变类型的循环策略。
我有一个 Scala 对象,其中包含初始主机列表和获取下一个主机的方法。
Object RoundRobin {
val optHosts: Option[List[String]] = Option[List[String]]("host1", "host2") // get from Configfile later
var hosts: List[String] = optHosts.getOrElse(List())
def url: String = {
hosts = hosts.tail ++ List(hosts.head)
hosts(0)
}
def execute = ??? // do something with the next host
}
我读过 Scala 中的不可变队列,但我真的不知道如何用不可变类型解决这个问题。不知何故,我必须记住一个索引,对吗?这是使用不可变类型没有意义的情况之一吗?
【问题讨论】:
-
你能澄清一下 OptHosts(特别是它的类型)吗?
-
当然,把它添加到示例代码中。
标签: scala round-robin