如何在 Scala 中返回迭代器?

发布于 2024-08-19 03:47:36 字数 39 浏览 10 评论 0原文

我必须做什么才能从方法/类返回迭代器?如何将这一特征添加到类中?

What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹沉浮 2024-08-26 03:47:36

您可以扩展 Iterator,这需要您实现nexthasNext 方法:

  class MyAnswer extends Iterator[Int] {
    def hasNext = true
    def next = 42
  }

但是,如果扩展 Iterable,它要求您实现 elements (或 2.8 中的 iterator):

  class MyAnswer extends Iterable[Int] {
    def iterator = new Iterator[Int] {
      def hasNext = true
      def next = 42
    }
  }

常见的习惯用法似乎是将迭代器公开给某些私有集合,如下所示:

  class MyStooges extends Iterable[String] {
    private val stooges = List("Moe", "Larry", "Curly")
    def iterator = stooges.iterator
  }

You can extend Iterator, which will require that you implement the next and hasNext methods:

  class MyAnswer extends Iterator[Int] {
    def hasNext = true
    def next = 42
  }

But, you will get more flexibility if you extend Iterable, which requires you implement elements (or iterator in 2.8):

  class MyAnswer extends Iterable[Int] {
    def iterator = new Iterator[Int] {
      def hasNext = true
      def next = 42
    }
  }

A common idiom seems to be to expose an iterator to some private collection, like this:

  class MyStooges extends Iterable[String] {
    private val stooges = List("Moe", "Larry", "Curly")
    def iterator = stooges.iterator
  }
蓬勃野心 2024-08-26 03:47:36

对于方法,只需 yield

def odd(from: Int, to: Int): List[Int] = 
  for (i <- List.range(from, to) if i % 2 == 1) yield i

For a method, just yield:

def odd(from: Int, to: Int): List[Int] = 
  for (i <- List.range(from, to) if i % 2 == 1) yield i
孤蝉 2024-08-26 03:47:36

这两个答案从下面的帖子中得到了帮助,并感谢@Dima。

假设您有一个类链表。而要求是打印列表中的所有元素。

trait LinkedList {
  def nodeValue: Int
  def tailList: LinkedList
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList

object Nil extends LinkedList {
  def nodeValue = throw new IllegalAccessException("head of Nil")
  def tailList = throw new IllegalAccessException("tail of Nil")
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)
print(chainedLinkedList)
A$A44$A$A44$Node@7b7a2c78res0: Unit = ()

现在让我们为这个类实现迭代器。

trait LinkedList extends Iterator[Int]{
  def nodeValue: Int
  def tailList: LinkedList
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList {
  var ptr: LinkedList = this

  //The following two are mandatory for extending Iterator
  override def hasNext: Boolean = ptr match { case Nil => false; case _=> true}

  override def next(): Int = {
    val result = ptr.nodeValue
    ptr = ptr.tailList
    result
  }
}

object Nil extends LinkedList {
  def nodeValue = throw new IllegalAccessException("head of Nil")
  def tailList = throw new IllegalAccessException("tail of Nil")

  //The following two are mandatory for extending Iterator
  override def hasNext: Boolean = false
  override def next(): Int = throw new IllegalAccessException("next of Nil")
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)

//Printing this first Time
chainedLinkedList.foreach(println)
//Prints 2 1

//Printing second Time
chainedLinkedList.foreach(println)
//No output

在迭代器实现中,一旦 ptr 到达末尾,它就无法前进。可迭代的实现解决了这个问题。

trait LinkedList extends Iterable[Int]{
  val nodeValue: Int
  val tailList: LinkedList
  override def toString(): String = this.mkString(" -> ")
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList {

  override def iterator: Iterator[Int] = Iterator
    .iterate(this: LinkedList)(_.tailList)
    .takeWhile(_ != Nil)
    .map(_.nodeValue)
}

object Nil extends LinkedList {
  lazy val nodeValue= throw new IllegalAccessException("head of Nil")
  lazy val tailList = throw new IllegalAccessException("tail of Nil")

  override def iterator: Iterator[Int] = Iterator.empty
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)

//Printing this first Time
chainedLinkedList.foreach(println)
Output 2 -> 1
chainedLinkedList.foreach(println)
Output 2 -> 1

These two answers had help from the posts below and thanks @Dima.

Lets assume you have a class linked list. And the requirement is to print all the elements in the list.

trait LinkedList {
  def nodeValue: Int
  def tailList: LinkedList
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList

object Nil extends LinkedList {
  def nodeValue = throw new IllegalAccessException("head of Nil")
  def tailList = throw new IllegalAccessException("tail of Nil")
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)
print(chainedLinkedList)
A$A44$A$A44$Node@7b7a2c78res0: Unit = ()

Now Lets implement iterator to this class.

trait LinkedList extends Iterator[Int]{
  def nodeValue: Int
  def tailList: LinkedList
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList {
  var ptr: LinkedList = this

  //The following two are mandatory for extending Iterator
  override def hasNext: Boolean = ptr match { case Nil => false; case _=> true}

  override def next(): Int = {
    val result = ptr.nodeValue
    ptr = ptr.tailList
    result
  }
}

object Nil extends LinkedList {
  def nodeValue = throw new IllegalAccessException("head of Nil")
  def tailList = throw new IllegalAccessException("tail of Nil")

  //The following two are mandatory for extending Iterator
  override def hasNext: Boolean = false
  override def next(): Int = throw new IllegalAccessException("next of Nil")
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)

//Printing this first Time
chainedLinkedList.foreach(println)
//Prints 2 1

//Printing second Time
chainedLinkedList.foreach(println)
//No output

In the iterator implementation, once ptr reached the end, it could did not advance back. Iterable implementation solves this.

trait LinkedList extends Iterable[Int]{
  val nodeValue: Int
  val tailList: LinkedList
  override def toString(): String = this.mkString(" -> ")
}

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList {

  override def iterator: Iterator[Int] = Iterator
    .iterate(this: LinkedList)(_.tailList)
    .takeWhile(_ != Nil)
    .map(_.nodeValue)
}

object Nil extends LinkedList {
  lazy val nodeValue= throw new IllegalAccessException("head of Nil")
  lazy val tailList = throw new IllegalAccessException("tail of Nil")

  override def iterator: Iterator[Int] = Iterator.empty
}

val singleLinkedList = new Node(1,Nil)
val chainedLinkedList = new Node(2,singleLinkedList)

//Printing this first Time
chainedLinkedList.foreach(println)
Output 2 -> 1
chainedLinkedList.foreach(println)
Output 2 -> 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文