是一个“无限"的迭代器设计不好?

Is an quot;infinitequot; iterator bad design?(是一个“无限的迭代器设计不好?)

本文介绍了是一个“无限"的迭代器设计不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供无限"的 Iterator 实现通常被认为是不好的做法吗?即对 hasNext() always(*) 的调用在哪里返回 true?

Is it generally considered bad practice to provide Iterator implementations that are "infinite"; i.e. where calls to hasNext() always(*) return true?

通常我会说是",因为调用代码可能会出现异常行为,但在下面的实现中,hasNext() 将返回 true,除非调用者从列表中删除迭代器所在的所有元素初始化为;即有一个终止条件.你认为这是对 Iterator 的合法使用吗?尽管我想有人可能会说它不直观,但它似乎并没有违反合同.

Typically I'd say "yes" because the calling code could behave erratically, but in the below implementation hasNext() will return true unless the caller removes all elements from the List that the iterator was initialised with; i.e. there is a termination condition. Do you think this is a legitimate use of Iterator? It doesn't seem to violate the contract although I suppose one could argue it's unintuitive.

public class CyclicIterator<T> implements Iterator<T> {
  private final List<T> l;
  private Iterator<T> it;

  public CyclicIterator<T>(List<T> l) {
    this.l = l;
    this.it = l.iterator();
  }

  public boolean hasNext() {
    return !l.isEmpty();
  }

  public T next() {
    T ret;

    if (!hasNext()) {
      throw new NoSuchElementException();
    } else if (it.hasNext()) {
      ret = it.next();
    } else {
      it = l.iterator();
      ret = it.next();
    }

    return ret;
  }

  public void remove() {
    it.remove();
  }
}

(迂腐)编辑

有些人评论了如何使用 Iterator 从无界序列(如斐波那契序列)生成值.但是,Java Iterator 文档指出 Iterator 是:

Some people have commented how an Iterator could be used to generate values from an unbounded sequence such as the Fibonacci sequence. However, the Java Iterator documentation states that an Iterator is:

集合上的迭代器.

现在你可以说斐波那契数列是一个无限集合,但在 Java 中我会将集合等同于 java.util.Collection 接口,它提供了诸如 size() 暗示一个集合必须是有界的.因此,使用 Iterator 作为无界序列的值生成器是否合法?

Now you could argue that the Fibonacci sequence is an infinite collection but in Java I would equate collection with the java.util.Collection interface, which offers methods such as size() implying that a collection must be bounded. Therefore, is it legitimate to use Iterator as a generator of values from an unbounded sequence?

推荐答案

我认为它完全合法 - Iterator 只是一个东西"流.为什么流必须有界?

I think it is entirely legitimate - an Iterator is just a stream of "stuff". Why should the stream necessarily be bounded?

许多其他语言(例如 Scala)都内置了无限流的概念,并且可以对其进行迭代.例如,使用 scalaz

Plenty of other languages (e.g. Scala) have the concept of unbounded streams built in to them and these can be iterated over. For example, using scalaz

scala> val fibs = (0, 1).iterate[Stream](t2 => t2._2 -> (t2._1 + t2._2)).map(_._1).iterator
fibs: Iterator[Int] = non-empty iterator

scala> fibs.take(10).mkString(", ") //first 10 fibonnacci numbers
res0: String = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

就最小意外原则而言,我认为这完全取决于上下文.例如,我希望这个方法返回什么?

public Iterator<Integer> fibonacciSequence();

这篇关于是一个“无限"的迭代器设计不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是一个“无限"的迭代器设计不好?

基础教程推荐