From e3d4efe21df2e2299e0cdfea7306a98937d61193 Mon Sep 17 00:00:00 2001 From: Michael Homer Date: Wed, 28 Feb 2018 17:28:26 +1300 Subject: [PATCH] Implement Enumerable.Repeat The external method Repeat(TResult,int) was not implemented. This commit introduces a test case and matching implementation returning a JSIL AbstractEnumerable returning the element the appropriate number of times. --- .../Linq/Classes/System.Linq.Enumerable.js | 26 +++++++++++++++++++ Tests/SimpleTestCases/EnumerableRepeat.cs | 13 ++++++++++ Tests/SimpleTests.csproj | 1 + 3 files changed, 40 insertions(+) create mode 100644 Tests/SimpleTestCases/EnumerableRepeat.cs diff --git a/JSIL.Libraries/Includes/Bootstrap/Linq/Classes/System.Linq.Enumerable.js b/JSIL.Libraries/Includes/Bootstrap/Linq/Classes/System.Linq.Enumerable.js index 5980d9d17..af120e6c9 100644 --- a/JSIL.Libraries/Includes/Bootstrap/Linq/Classes/System.Linq.Enumerable.js +++ b/JSIL.Libraries/Includes/Bootstrap/Linq/Classes/System.Linq.Enumerable.js @@ -557,6 +557,32 @@ } ); + $.Method({ Static: true, Public: true }, "Repeat", + new JSIL.MethodSignature( + $jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", ["!!0"]), + ["!!0", "System.Int32"], + ["TResult"]), + function Repeat$b1(TResult, element, count) { + var i = 0; + + return new (JSIL.AbstractEnumerable.Of(TResult))( + function getNext(result) { + if (i < count) { + i++; + result.set(element); + return true; + } + return false; + }, + function reset() { + i = 0; + }, + function dispose() { + } + ); + } + ); + $.Method({ Static: true, Public: true }, "Where", new JSIL.MethodSignature($jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", ["!!0"]), [$jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", ["!!0"]), $jsilcore.TypeRef("System.Func`2", ["!!0", $.Boolean])], ["TSource"]), function Where$b1(TSource, source, predicate) { diff --git a/Tests/SimpleTestCases/EnumerableRepeat.cs b/Tests/SimpleTestCases/EnumerableRepeat.cs new file mode 100644 index 000000000..6c64a22bb --- /dev/null +++ b/Tests/SimpleTestCases/EnumerableRepeat.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public static class Program { + public static void Main () { + + foreach (var x in Enumerable.Repeat("x", 5)) + Console.WriteLine(x); + + } +} diff --git a/Tests/SimpleTests.csproj b/Tests/SimpleTests.csproj index dc6faeada..f021964fd 100644 --- a/Tests/SimpleTests.csproj +++ b/Tests/SimpleTests.csproj @@ -311,6 +311,7 @@ +