title: Implementing IEnumerable in JavaScript / TypeScript
body: <p>I'm trying to implement the C# keyword <code>yield</code> with  in JavaScript/TypeScript (doesn't matter which): For example, I would like to implement <a href="http://msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspx" rel="nofollow noreferrer">the code</a>:</p>
  
  <pre><code>//using System.Collections;  
  //using System.Diagnostics; 
  public static void Process()
  {
      // Display powers of 2 up to the exponent of 8:  
      foreach (int number in Power(2, 8))
      {
          Debug.Write(number.ToString() + " ");
      }
      // Output: 2 4 8 16 32 64 128 256
  }
  
  
  public static IEnumerable Power(int baseNumber, int highExponent)
  {
      int result = 1;
  
      for (int counter = 1; counter &lt;= highExponent; counter++)
      {
          result = result * baseNumber;
          yield return result;
      }
  }
  </code></pre>
  
  <p>in JavaScript.</p>
  
  <p>The end goal is to implement a function written in C# from another <a href="https://stackoverflow.com/questions/13548048/is-there-a-splitbycharactertype-method-in-c-sharp-like-there-is-in-java">question I asked about on stackoverflow</a>, in JavaScript:</p>
  
  <pre><code>public static IEnumerable&lt;string&gt; SplitByCharacterType(string input)
  {
      if (String.IsNullOrEmpty(input))
          throw new ArgumentNullException(nameof(input));
  
      StringBuilder segment = new StringBuilder();
      segment.Append(input[0]);
      var current = Char.GetUnicodeCategory(input[0]);
  
      for (int i = 1; i &lt; input.Length; i++)
      {
          var next = Char.GetUnicodeCategory(input[i]);
          if (next == current)
          {
              segment.Append(input[i]);
          }
          else
          {
              yield return segment.ToString();
              segment.Clear();
              segment.Append(input[i]);
              current = next;
          }
      }
      yield return segment.ToString();
  }
  </code></pre>
  
  <p>Any ideas?</p>