What is the difference between an Iterator and a Generator?

后端 未结 9 1790
感情败类
感情败类 2020-12-07 15:49

What is the difference between an Iterator and a Generator?

相关标签:
9条回答
  • 2020-12-07 16:14

    An iterator is commonly used to move through a collection of items. Often having MoveNext() and Current() methods. MoveNext() would shift the pointer to the next collection item (if possible) and return true/false based on success. Current() would provide the actual value.

    A generator is an implementation of iterator, but instead of pointing to a pre-existing collection, it creates new items on each MoveNext() call.

    0 讨论(0)
  • 2020-12-07 16:15

    There's too much Python here, and too many people saying generators are the only way to implement an infinite iterator. Here's the example I mentioned (squares of all natural numbers) implemented in C#. ExplicitSquares explicitly implements an iterator (called IEnumerator in C#). ImplicitSquares uses a generator to do the same thing. Both are infinite iterators and have no backing collection. The only difference is whether the state machine is spelled out, or alternatively a generator is used.

    using System.Collections;
    using System.Collections.Generic;
    using System;
    
    class ExplicitSquares : IEnumerable<int>
    {
        private class ExplicitSquaresEnumerator : IEnumerator<int>
        {
            private int counter = 0;
    
            public void Reset()
            {
                counter = 0;
            }
    
            public int Current { get { return counter * counter; }}
    
            public bool MoveNext()
            {
                counter++;
                return true;
            }
    
            object IEnumerator.Current { get { return Current; } }
    
            public void Dispose(){}
        }
    
        public IEnumerator<int> GetEnumerator()
        {
            return new ExplicitSquaresEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    
    class ImplicitSquares : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
            int counter = 1;
            while(true)
            {
                int square = counter * counter;
                yield return square;
                counter++;
            }
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    
    public class AllSquares
    {
        private static readonly int MAX = 10;
    
        public static void Main()
        {
            int i = 0;
            foreach(int square in new ExplicitSquares())
            {
                i++;
                if(i >= MAX)
                    break;
                Console.WriteLine(square);
            }
    
            Console.WriteLine();
    
            int j = 0;
            foreach(int square in new ImplicitSquares())
            {
                j++;
                if(j >= MAX)
                    break;
                Console.WriteLine(square);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 16:15

    A Generator is a special function that can behave as an Iterator, returning a value each time it is called. Because it is a function, it can compute each value on demand. And because it is special, it can remember its state from the last time it was called, so the resulting code looks pretty simple.

    For example, this generator in python will produce a sequence of integers

    def integers():
        int n = 0
        while True:
            yield n
            n += 1
    

    The important thing in this example is the yield n statement. The function will return the value, and the next time it is called, it will continue from that point.

    This link has a longer explanation of generators in python: link text

    0 讨论(0)
  • 2020-12-07 16:16

    An iterator traverses a collection one at a time.

    A generator generates a sequence, one item at a time.

    You might for example, iterate over the result of a generator...

    0 讨论(0)
  • 2020-12-07 16:28

    (from javascript useland, yet same as all others)

    An interator is an object that has a .next() function

    A generator is a function, once invoked, produce an iterator, it's a factory for iterator.

    In javascript, generator function require a special syntax function *(){} and the use for the yield keyword

    See MDN on this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

    0 讨论(0)
  • 2020-12-07 16:31

    Usually iterators walk over an existing sequence (such as an array or list) and generators calculate a new value upon every request.

    0 讨论(0)
提交回复
热议问题