如何从scala迭代器[T]转换为选项[T]

如何从scala迭代器[T]转换为选项[T],scala,scala-collections,scala-2.10,Scala,Scala Collections,Scala 2.10,我尝试将一个迭代器转换为一个等价的选项,该迭代器应该返回单个项 我所能做的就是这样。我应该使用标准API中的东西吗 def toUniqueOption[T](a: Iterator[T]): Option[T] = if (a.size > 1) throw new RuntimeException("The iterator should be emtpy or contain a single item but contained ${a.size} items.

我尝试将一个迭代器转换为一个等价的选项,该迭代器应该返回单个项

我所能做的就是这样。我应该使用标准API中的东西吗

def toUniqueOption[T](a: Iterator[T]): Option[T] =
    if (a.size > 1)
      throw new RuntimeException("The iterator should be emtpy or contain a single item but contained ${a.size} items.")
    else if (a.size > 0)
      Option(a.toList(0))
    else
      Option.empty

用try更新

def toUnique[T](a: Iterator[T]): Try[Option[T]] =
    if (a.size > 1)
      Failure(new RuntimeException("The iterator should be emtpy or contain a single item but contained ${a.size} items."))
    else if (a.size > 0)
      Success(Option(a.toList(0)))
    else
      Success(Option.empty)

调用
size
是有风险的,因为它不能保证有效甚至停止

那么:

def toUniqueOption[T](a: Iterator[T]): Option[T] =
  a.take(2).toList match {
    case Nil => None
    case x :: Nil => Some(x)
    case _ => throw new RuntimeException("Iterator size > 1")
  }

您可以使用
hasNext
next
避免迭代整个序列:

def toUniqueOption[T](a: Iterator[T]): Option[T] = {
    if(a.hasNext) {
        val f = a.next()
        if(a.hasNext) throw new RuntimeException("Iterator should contain at most one element")
        Some(f)
    }
    else None
}

这不完全是你想要的,但为什么不使用类似于:

  def nextAsOption[T](i: Iterator[T]) : Option[T] = {
    i.hasNext match {
      case true  => Some(i.next)
      case false => None
    }
  }

这只是为您提供了一个迭代器“next”操作,该操作返回一个选项而不是布尔值。当您需要传递选项时非常方便。

您实际上可以使用标准API:

a.toStream.headOption
其中a:迭代器[T]


编辑:在scala 2.13+中,只需使用
a.nextOption()

使用异常来表示逻辑是个坏主意。请考虑类型<代码> [字符串,选项[t] ] < />代码。我将尝试使用我看到可以转换为选项[T]的try[T],如果(I.hasNext)有一些(I.next),为什么不
就我所知,它在效果和编译器优化方面完全相同。我支持使用简单的
if
来提高可读性。注意:
Stream
同时变成了
LazyList
,你现在应该使用
a.to(LazyList)。headOption
。但事实证明,你也可以(也应该)只做
a.nextOption()