From 28ea108c484568bc626a472b3debad48b92ec6e2 Mon Sep 17 00:00:00 2001 From: Izumemori Date: Tue, 1 Oct 2019 23:10:07 +0200 Subject: [PATCH 1/3] Improved C# Rube --- CSharp/csharp_rube.cs | 64 ----------------------------------- CSharp/rube.cs | 77 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 64 deletions(-) delete mode 100644 CSharp/csharp_rube.cs create mode 100644 CSharp/rube.cs diff --git a/CSharp/csharp_rube.cs b/CSharp/csharp_rube.cs deleted file mode 100644 index 84a69ac..0000000 --- a/CSharp/csharp_rube.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Linq; - -namespace Hello_Rube -{ - class Program - { - static void Main(string[] args) - { - // Hello World - const string word = "Hello World"; - - // Define How Much Rail We Wanted - char[][] rails = new char[3][]; - - // Set The Character Array in Each Rail - for (int i = 0; i < rails.Length; i++) - { - // Each Rail Has Same Length With word Variable Length - rails[i] = new char[word.Length]; - - // Each Array Index, Assign "." Value - for (int j = 0; j < rails[i].Length; j++) - { - rails[i][j] = '.'; - } - } - - /* Helper Variable */ - int r = 0; - - bool down = true; - - /** - * Enumarate Using LINQ To Get Index and Value - * Even Loop With Foreach */ - word.Select((Value, Index) => new { Value, Index }) - .ToList().ForEach( - c => - { - rails[r][c.Index] = c.Value; - r = down == true ? r += 1 : r -= 1; - - if (r == 2) - down = false; - else if (r == 0) - down = true; - }); - - /** Output */ - rails.ToList().ForEach(rail => - { - Console.WriteLine(String.Join(" ", rail)); - }); - - /** - * Because I'm Using Visual Studio, We Need This Line Below - * To Make Terminal Not Immediately Exit After Running The Program - * This Line Can Be Remove - * If You Are Run This Program In Command Prompt */ - Console.ReadKey(); - } - } -} diff --git a/CSharp/rube.cs b/CSharp/rube.cs new file mode 100644 index 0000000..8a96284 --- /dev/null +++ b/CSharp/rube.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace Rube +{ + class Program + { + private readonly byte[] _word = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2C, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x2E, 0x0D, 0x0A }; + private readonly ConcurrentCharacterConverter _converter = new ConcurrentCharacterConverter(); + public static Task Main(string[] args) => new Program().StartAsync(); + + private async Task StartAsync() + { + await foreach (var c in GetChars(this._word)) + { + var output = new CharWrapper(c) switch { + CharWrapper { Char: var x, IsDigit: true } => string.Empty, + CharWrapper { Char: var x, IsControl: true } => x.ToString(), + CharWrapper { Char: var x, IsLower: true } => x.ToString(), + CharWrapper { Char: var x, IsPunctuation: true} => x == '.' ? "." : ",", + CharWrapper { Char: var x } => x.ToString() + }; + + if (!string.IsNullOrEmpty(output)) + Console.Write(output); + } + } + + private async IAsyncEnumerable GetChars(byte[] array) + { + foreach (var c in array) + { + yield return await this._converter.ConvertAsync(c); + } + } + } + + public readonly struct CharWrapper + { + public char Char { get; } + public bool IsDigit => char.IsDigit(this.Char); + public bool IsControl => char.IsControl(this.Char); + public bool IsLower => char.IsLower(this.Char); + public bool IsPunctuation => char.IsPunctuation(this.Char); + + public CharWrapper(char c) + => this.Char = c; + } + + public class ConcurrentCharacterConverter + { + private readonly SemaphoreSlim _semaphore; + + public ConcurrentCharacterConverter() + { + this._semaphore = new SemaphoreSlim(1, 1); + } + + public Task ConvertAsync(From input) + { + return Task.Run(async () => { + try + { + await this._semaphore.WaitAsync(); + return (To)Convert.ChangeType(input, typeof(To)); + } + finally + { + this._semaphore.Release(); + } + }); + } + } +} From 7d764fdec2c17a78d11684fee9c0a5943ab22950 Mon Sep 17 00:00:00 2001 From: Izumemori Date: Tue, 1 Oct 2019 23:16:03 +0200 Subject: [PATCH 2/3] Yikes --- CSharp/rube.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CSharp/rube.cs b/CSharp/rube.cs index 8a96284..ef2c9e2 100644 --- a/CSharp/rube.cs +++ b/CSharp/rube.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -31,9 +32,11 @@ private async Task StartAsync() private async IAsyncEnumerable GetChars(byte[] array) { - foreach (var c in array) + var tasks = array.Select(x => this._converter.ConvertAsync(x)); + + foreach (var task in tasks) { - yield return await this._converter.ConvertAsync(c); + yield return await task; } } } From a0837564c5b5716a40db1bc84ba9bcfeb7fc93f3 Mon Sep 17 00:00:00 2001 From: Izumemori Date: Tue, 1 Oct 2019 23:21:42 +0200 Subject: [PATCH 3/3] Undelete file --- CSharp/csharp_rube.cs | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 CSharp/csharp_rube.cs diff --git a/CSharp/csharp_rube.cs b/CSharp/csharp_rube.cs new file mode 100644 index 0000000..84a69ac --- /dev/null +++ b/CSharp/csharp_rube.cs @@ -0,0 +1,64 @@ +using System; +using System.Linq; + +namespace Hello_Rube +{ + class Program + { + static void Main(string[] args) + { + // Hello World + const string word = "Hello World"; + + // Define How Much Rail We Wanted + char[][] rails = new char[3][]; + + // Set The Character Array in Each Rail + for (int i = 0; i < rails.Length; i++) + { + // Each Rail Has Same Length With word Variable Length + rails[i] = new char[word.Length]; + + // Each Array Index, Assign "." Value + for (int j = 0; j < rails[i].Length; j++) + { + rails[i][j] = '.'; + } + } + + /* Helper Variable */ + int r = 0; + + bool down = true; + + /** + * Enumarate Using LINQ To Get Index and Value + * Even Loop With Foreach */ + word.Select((Value, Index) => new { Value, Index }) + .ToList().ForEach( + c => + { + rails[r][c.Index] = c.Value; + r = down == true ? r += 1 : r -= 1; + + if (r == 2) + down = false; + else if (r == 0) + down = true; + }); + + /** Output */ + rails.ToList().ForEach(rail => + { + Console.WriteLine(String.Join(" ", rail)); + }); + + /** + * Because I'm Using Visual Studio, We Need This Line Below + * To Make Terminal Not Immediately Exit After Running The Program + * This Line Can Be Remove + * If You Are Run This Program In Command Prompt */ + Console.ReadKey(); + } + } +}