Selaa lähdekoodia

Adding Playground console application.

Bart De Smet 8 vuotta sitten
vanhempi
sitoutus
68bc36205f

+ 21 - 0
Ix.NET/Source/Ix.NET.sln

@@ -39,6 +39,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Linq.Async.Queryable
 EndProject
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{80EFE3A1-1414-42EA-949B-1B5370A1B2EA}"
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{80EFE3A1-1414-42EA-949B-1B5370A1B2EA}"
 EndProject
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dev", "dev", "{61683064-A120-44A7-A174-E19154F6D84F}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Playground", "Playground\Playground.csproj", "{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}"
+EndProject
 Global
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		Debug|Any CPU = Debug|Any CPU
@@ -203,6 +207,22 @@ Global
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x64.Build.0 = Release|Any CPU
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x64.Build.0 = Release|Any CPU
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x86.ActiveCfg = Release|Any CPU
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x86.ActiveCfg = Release|Any CPU
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x86.Build.0 = Release|Any CPU
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C}.Release|x86.Build.0 = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|ARM.ActiveCfg = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|ARM.Build.0 = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|x64.ActiveCfg = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|x64.Build.0 = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Debug|x86.Build.0 = Debug|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|Any CPU.Build.0 = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|ARM.ActiveCfg = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|ARM.Build.0 = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|x64.ActiveCfg = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|x64.Build.0 = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|x86.ActiveCfg = Release|Any CPU
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0}.Release|x86.Build.0 = Release|Any CPU
 	EndGlobalSection
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 		HideSolutionNode = FALSE
@@ -217,6 +237,7 @@ Global
 		{172BD8C4-5C3E-4928-9D3F-746CF336FFEC} = {87534290-A7A6-47A4-9A3A-D0D21A9AD1D4}
 		{172BD8C4-5C3E-4928-9D3F-746CF336FFEC} = {87534290-A7A6-47A4-9A3A-D0D21A9AD1D4}
 		{9B1E5420-E8F3-4B5F-A11A-4D18578F50CE} = {80EFE3A1-1414-42EA-949B-1B5370A1B2EA}
 		{9B1E5420-E8F3-4B5F-A11A-4D18578F50CE} = {80EFE3A1-1414-42EA-949B-1B5370A1B2EA}
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C} = {80EFE3A1-1414-42EA-949B-1B5370A1B2EA}
 		{513F9ABD-4FB8-4AC1-89DA-C3300399F34C} = {80EFE3A1-1414-42EA-949B-1B5370A1B2EA}
+		{CFDA4685-B2AF-46FE-81C1-55A291B9B1B0} = {61683064-A120-44A7-A174-E19154F6D84F}
 	EndGlobalSection
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {AF70B0C6-C9D9-43B1-9BE4-08720EC1B7B7}
 		SolutionGuid = {AF70B0C6-C9D9-43B1-9BE4-08720EC1B7B7}

+ 21 - 0
Ix.NET/Source/Playground/DemoAttribute.cs

@@ -0,0 +1,21 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System;
+
+namespace Playground
+{
+    [AttributeUsage(AttributeTargets.Method)]
+    internal sealed class DemoAttribute : Attribute
+    {
+        public DemoAttribute(int index, string title)
+        {
+            Index = index;
+            Title = title;
+        }
+
+        public int Index { get; }
+        public string Title { get; }
+    }
+}

+ 17 - 0
Ix.NET/Source/Playground/Playground.csproj

@@ -0,0 +1,17 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.0</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\System.Interactive.Async.Providers\System.Interactive.Async.Providers.csproj" />
+    <ProjectReference Include="..\System.Interactive.Async\System.Interactive.Async.csproj" />
+    <ProjectReference Include="..\System.Interactive.Providers\System.Interactive.Providers.csproj" />
+    <ProjectReference Include="..\System.Interactive\System.Interactive.csproj" />
+    <ProjectReference Include="..\System.Linq.Async.Queryable\System.Linq.Async.Queryable.csproj" />
+    <ProjectReference Include="..\System.Linq.Async\System.Linq.Async.csproj" />
+  </ItemGroup>
+
+</Project>

+ 185 - 0
Ix.NET/Source/Playground/Program.cs

@@ -0,0 +1,185 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+
+namespace Playground
+{
+    class Program
+    {
+        static void Main()
+        {
+            RunDemos();
+        }
+
+        [Demo(0, "Random experimentation")]
+        static async Task Experiment()
+        {
+            // Add test code here
+
+            await Task.Yield();
+        }
+
+        [Demo(1, "LINQ to Objects for IEnumerable<T>")]
+        static void Linq()
+        {
+            var xs = new List<int> { 1, 2, 3 };
+            var ys = xs.Where(x => x % 2 == 0);
+
+            foreach (var y in ys)
+            {
+                Console.WriteLine(y);
+            }
+        }
+
+        [Demo(2, "LINQ to Objects for IEnumerable<T> - Interactive Extensions")]
+        static void Ix()
+        {
+            var xs = new List<int> { 1, 2, 3 };
+            var ys = xs.Distinct(x => x % 2);
+
+            foreach (var y in ys)
+            {
+                Console.WriteLine(y);
+            }
+        }
+
+        [Demo(3, "LINQ to Objects for IAsyncEnumerable<T>")]
+        static async Task AsyncLinq()
+        {
+            var xs = new List<int> { 1, 2, 3 };
+            var ys = xs.ToAsyncEnumerable().Where(x => x % 2 == 0);
+
+#if CSHARP8
+            foreach await (var y in ys)
+            {
+                Console.WriteLine(y);
+            }
+#else
+            var e = ys.GetAsyncEnumerator();
+
+            try
+            {
+                while (await e.MoveNextAsync())
+                {
+                    var y = e.Current;
+
+                    Console.WriteLine(y);
+                }
+            }
+            finally
+            {
+                await e.DisposeAsync();
+            }
+#endif
+        }
+
+        [Demo(4, "LINQ to Objects for IAsyncEnumerable<T> - Interactive Extensions")]
+        static async Task AsyncIx()
+        {
+            var xs = new List<int> { 1, 2, 3 };
+            var ys = xs.ToAsyncEnumerable().Distinct(x => x % 2);
+
+#if CSHARP8
+            foreach await (var y in ys)
+            {
+                Console.WriteLine(y);
+            }
+#else
+            await ys.ForEachAsync(y =>
+            {
+                Console.WriteLine(y);
+            });
+#endif
+        }
+
+        static void RunDemos()
+        {
+            var methods = (from method in typeof(Program).GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
+                           let demo = method.GetCustomAttribute<DemoAttribute>()
+                           let parameters = method.GetParameters()
+                           let returnType = method.ReturnType
+                           where demo != null && parameters.Length == 0 && (returnType == typeof(void) || returnType == typeof(Task))
+                           orderby demo.Index
+                           select new { Demo = demo, Invoke = GetInvoker(method) })
+                          .ToArray();
+
+            var invokers = methods.ToDictionary(m => m.Demo.Index, m => m.Invoke);
+
+            while (true)
+            {
+                foreach (var method in methods)
+                {
+                    Console.WriteLine($"{method.Demo.Index}. {method.Demo.Title}");
+                }
+
+                Console.WriteLine();
+
+                var retry = true;
+
+                while (retry)
+                {
+                    Console.Write("Enter demo [C: Clear, X: Exit]: ");
+                    var input = Console.ReadLine().Trim().ToUpper();
+
+                    switch (input)
+                    {
+                        case "C":
+                            retry = false;
+                            Console.Clear();
+                            break;
+                        case "X":
+                            return;
+                        default:
+                            if (!int.TryParse(input, out var i) || !invokers.TryGetValue(i, out var invoke))
+                            {
+                                Console.ForegroundColor = ConsoleColor.Red;
+                                Console.WriteLine("Invalid input.");
+                                Console.ResetColor();
+                            }
+                            else
+                            {
+                                retry = false;
+
+                                Console.ForegroundColor = ConsoleColor.Cyan;
+
+                                try
+                                {
+                                    invoke();
+                                }
+                                catch (Exception ex)
+                                {
+                                    Console.ForegroundColor = ConsoleColor.Red;
+                                    Console.WriteLine(ex.Message);
+                                }
+                                finally
+                                {
+                                    Console.ResetColor();
+                                }
+                            }
+
+                            break;
+                    }
+                }
+            }
+
+            Action GetInvoker(MethodInfo method)
+            {
+                if (method.ReturnType == typeof(void))
+                {
+                    return (Action)method.CreateDelegate(typeof(Action));
+                }
+                else
+                {
+                    var invoke = (Func<Task>)method.CreateDelegate(typeof(Func<Task>));
+                    return () => invoke().GetAwaiter().GetResult();
+                }
+            }
+        }
+    }
+}