/snippets/ComputationExpressions/ComputationExpressions/Enumerable/AppendEnumerable.n

http://github.com/saidai-no/nemerle · Unknown · 106 lines · 93 code · 13 blank · 0 comment · 0 complexity · 96ce955d3ba7dacf01cca9d8e1d3444d MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 David Sorokin <david.sorokin@gmail.com>
  3. * Copyright (c) 2010 rampelstinskin@gmail.com
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  18. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  20. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using Nemerle;
  27. using Nemerle.Utility;
  28. using Nemerle.Collections;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. namespace Nemerle.ComputationExpressions.Internal
  33. {
  34. /// <summary>
  35. /// Appends two enumerations using the tail call optimization.
  36. /// </summary>
  37. [Record]
  38. public class AppendEnumerable[T] : IEnumerable[T]
  39. {
  40. private ts1 : IEnumerable[T];
  41. private ts2 : IEnumerable[T];
  42. public GetEnumerator () : IEnumerator[T]
  43. {
  44. SubEnumerator (ts1.GetEnumerator (), () => ts2.GetEnumerator ())
  45. }
  46. public class SubEnumerator : IEnumerator[T]
  47. {
  48. private mutable ts : IEnumerator[T];
  49. private mutable cont : void -> IEnumerator[T];
  50. public this (ts : IEnumerator[T], cont : void -> IEnumerator[T])
  51. {
  52. this.ts = ts;
  53. this.cont = cont;
  54. }
  55. public Reset () : void
  56. {
  57. throw NotSupportedException ()
  58. }
  59. public Dispose () : void
  60. {
  61. ts.Dispose ();
  62. }
  63. public MoveNext () : bool
  64. {
  65. if (ts.MoveNext ())
  66. true
  67. else if (cont != null)
  68. {
  69. ts.Dispose ();
  70. ts = cont ();
  71. cont = null;
  72. match (ts)
  73. {
  74. | subts is SubEnumerator =>
  75. ts = subts.ts;
  76. cont = subts.cont;
  77. | _ => ()
  78. }
  79. MoveNext ();
  80. }
  81. else
  82. false
  83. }
  84. public Current : T
  85. {
  86. get
  87. {
  88. ts.Current
  89. }
  90. }
  91. }
  92. }
  93. }