-
-
Save faisalmansoor/6301115 to your computer and use it in GitHub Desktop.
Iterating using enumerator and looping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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