Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- namespace ConsoleApplication1
- {
- internal class Program
- {
- public static void Main()
- {
- Console.WriteLine("- Exponential -");
- foreach (var item in new Exponential().Take(4))
- {
- Console.WriteLine(item);
- }
- Console.WriteLine("ElementGenerationsCount: " + Exponential.ElementGenerationsCount);
- Console.WriteLine("- Exponential with changes -");
- foreach (var item in Test( new Exponential().Take(4)))
- {
- Console.WriteLine(item);
- }
- Console.WriteLine("ElementGenerationsCount: " + Exponential.ElementGenerationsCount);
- }
- static IEnumerable<int> Test(IEnumerable<int> sequence)
- {
- // генерация новых элеменов не происходит
- var enumerator = sequence.GetEnumerator();
- while (enumerator.MoveNext())
- {
- var item = enumerator.Current;
- item += 1; // изменяем значение последовательности
- yield return item;
- }
- }
- }
- public class Exponential : IEnumerable<int>
- {
- public Exponential() { ElementGenerationsCount = 0; }
- public static int ElementGenerationsCount { get; private set; }
- public IEnumerator<int> GetEnumerator()
- {
- var value = 1;
- while (true)
- {
- ElementGenerationsCount++;
- yield return value;
- value *= 2;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- public class Fibonacci : IEnumerable<int>
- {
- public IEnumerator<int> GetEnumerator()
- {
- var currentValue = 1;
- var previousValue = 1;
- yield return currentValue;
- yield return previousValue;
- while (true)
- {
- var newValue = currentValue + previousValue;
- previousValue = currentValue;
- currentValue = newValue;
- yield return currentValue;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement