This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifiy multiplication table to use async LINQ
Also makes it an async enumerable instead of a static class so it's nicer to invoke. Add a one-line multiplication table algorithm (unused). Prime generator is no longer a synchronous enumerable now that we have async LINQ.
- Loading branch information
Showing
8 changed files
with
74 additions
and
108 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,83 @@ | ||
namespace ClassLibrary1 | ||
{ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public static class MultiplicationTable | ||
public sealed class MultiplicationTable : IAsyncEnumerable<IAsyncEnumerable<int?>> | ||
{ | ||
public static async IAsyncEnumerable<IAsyncEnumerable<int?>> GenerateAsync(int count, [EnumeratorCancellation]CancellationToken cancellationToken = default, PrimeGeneratorOptions options = PrimeGeneratorOptions.None) | ||
private readonly int count; | ||
private readonly PrimeGeneratorOptions options; | ||
|
||
public MultiplicationTable(int count) | ||
: this(count, PrimeGeneratorOptions.None) | ||
{ | ||
} | ||
|
||
public MultiplicationTable(int count, PrimeGeneratorOptions options) | ||
{ | ||
this.count = count; | ||
this.options = options; | ||
} | ||
|
||
public async IAsyncEnumerator<IAsyncEnumerable<int?>> GetAsyncEnumerator(CancellationToken cancellationToken = default) | ||
{ | ||
// header row | ||
yield return GenerateRowAsync(count, options, cancellationToken); | ||
yield return this.GenerateRowAsync(cancellationToken); | ||
|
||
var primes = new PrimeGenerator(options).GetAsyncEnumerator(cancellationToken); | ||
var primes = new PrimeGenerator(this.options).Take(this.count); | ||
|
||
// rest of rows | ||
for (var i = 0; i < count && await primes.MoveNextAsync(); i++) | ||
await foreach (var prime in primes.WithCancellation(cancellationToken)) | ||
{ | ||
yield return GenerateRowAsync(count, options, cancellationToken, primes.Current); | ||
yield return this.GenerateRowAsync(cancellationToken, prime); | ||
} | ||
} | ||
|
||
private static async IAsyncEnumerable<int?> GenerateRowAsync(int count, PrimeGeneratorOptions options, [EnumeratorCancellation]CancellationToken cancellationToken = default, int? header = null) | ||
private async IAsyncEnumerable<int?> GenerateRowAsync([EnumeratorCancellation] CancellationToken cancellationToken, int? header = null) | ||
{ | ||
// header column cell | ||
yield return header; | ||
|
||
var primes = new PrimeGenerator(options).GetAsyncEnumerator(cancellationToken); | ||
var primes = new PrimeGenerator(this.options).Take(this.count); | ||
|
||
// rest of cells | ||
for (var i = 0; i < count && await primes.MoveNextAsync(); i++) | ||
await foreach (var prime in primes.WithCancellation(cancellationToken)) | ||
{ | ||
yield return (header ?? 1) * primes.Current; | ||
yield return (header ?? 1) * prime; | ||
} | ||
} | ||
|
||
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "It's really just for fun")] | ||
private static ConfiguredCancelableAsyncEnumerable<IAsyncEnumerable<int?>> OneLinerJustForFun(int count, CancellationToken cancellationToken = default, PrimeGeneratorOptions options = PrimeGeneratorOptions.None) => | ||
/* Elements of the produced table as they | ||
* relate to lines in the algorithm below: | ||
* | ||
* @4 @5 | ||
* ⭨ ┌─────┴─────┐ | ||
* ┌ ␀ 2 3 5 7 | ||
* │ 2 4 6 10 14 | ||
* @3 ┤ 3 6 9 15 21 | ||
* │ 5 10 15 25 35 | ||
* └ 7 14 21 35 49 | ||
* ↑ | ||
* @11 | ||
*/ | ||
|
||
/* 1: */ new PrimeGenerator(options) | ||
/* 2: */ .Take(count) // number of rows | ||
/* 3: */ .Select(prime => (int?)prime) // make nullable for topmost cell | ||
/* 4: */ .Prepend(null) // column header (topmost) cell | ||
/* 5: */ .Select(rowHeader => // rows | ||
/* 6: */ new PrimeGenerator(options) | ||
/* 7: */ .Take(count) // number of columns | ||
/* 8: */ .Select(prime => (int?)prime) // make nullable for leftmost cell | ||
/* 9: */ .Select(columnHeader => // cells | ||
/* 10: */ (rowHeader ?? 1) * columnHeader) // coalesce multiplicative identity for headerless top row | ||
/* 11: */ .Prepend(rowHeader)) // row header (leftmost) cell | ||
/* 12: */ .WithCancellation(cancellationToken); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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