Skip to content

Instantly share code, notes, and snippets.

@faisalmansoor
Created August 21, 2013 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faisalmansoor/6301115 to your computer and use it in GitHub Desktop.
Save faisalmansoor/6301115 to your computer and use it in GitHub Desktop.
Iterating using enumerator and looping
namespace FSharpHelper
{
[CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)]
[RequireQualifiedAccess]
[CompilationMapping(SourceConstructFlags.Module)]
public class MathHelper
{
[CompilationSourceName("sumEnum")]
public static T SumEnum<T>(FSharpList<T> list)
{
IEnumerator<T> enumerator = ((IEnumerable<T>)list).GetEnumerator();
try
{
T x = LanguagePrimitives.GenericZeroDynamic<T>();
while (enumerator.MoveNext())
x = LanguagePrimitives.AdditionDynamic<T, T, T>(x, enumerator.Current);
return x;
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
}
[CompilationArgumentCounts(new int[] { 1, 1 })]
[CompilationSourceName("sumLoop")]
public static T SumLoop<T>(FSharpList<T> xs)
{
// ISSUE: reference to a compiler-generated field
T s = default(T);
while (xs.TailOrNull != null)
{
FSharpList<T> fsharpList1 = xs;
// ISSUE: reference to a compiler-generated field
FSharpList<T> fsharpList2 = fsharpList1.Tail;
// ISSUE: reference to a compiler-generated field
T obj = fsharpList1.Head;
T state = LanguagePrimitives.AdditionDynamic<T, T, T>(s, obj);
xs = fsharpList2;
s = state;
}
return s;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment