Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /// <summary>
  2. /// 無限リスト
  3. /// </summary>
  4. /// <typeparam name="T"></typeparam>
  5. class InfiniteList<T> : IEnumerable<T>
  6. {
  7. private readonly List<T> _list;
  8.  
  9. public InfiniteList(IEnumerable<T> source)
  10. {
  11. _list = source.ToList();
  12. }
  13.  
  14. public IEnumerator<T> GetEnumerator()
  15. {
  16. while (true)
  17. {
  18. foreach (T val in _list)
  19. {
  20. yield return val;
  21. }
  22. }
  23. }
  24.  
  25. IEnumerator IEnumerable.GetEnumerator()
  26. {
  27. return this.GetEnumerator();
  28. }
  29. }
  30.  
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. // 繰り返しな無限リスト
  36. int[] a12345 = new[] { 1, 2, 3, 4, 5 };
  37. InfiniteList<int> infiniteInt = new InfiniteList<int>(a12345);
  38. foreach(var x in infiniteInt.Take(20))
  39. {
  40. Console.WriteLine(x);
  41. }
  42.  
  43. // ランダムな無限リスト
  44. var rnd = new Random();
  45. Func<int>[] r = new Func<int>[] { () => rnd.Next() };
  46. InfiniteList<Func<int>> infiniteFunc = new InfiniteList<Func<int>>(r);
  47. foreach(var x in infiniteFunc.Take(20))
  48. {
  49. Console.WriteLine(x());
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement