Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TraversalExtensions sample notebook #192

Merged
merged 21 commits into from
Oct 19, 2022
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
afbf08e
TraversalExtensions sample notebook
ElisendaGascon Sep 20, 2022
5ecf406
Addressed 3/5 PR comments
jesshill99 Sep 22, 2022
5beba8a
Updated ForEachFailEndAsync() description
jesshill99 Sep 22, 2022
cfd570b
Add explanation to `ForEachAsync()`
ElisendaGascon Sep 23, 2022
7bd08a0
Add "How to use .NET Interactive notebooks" section
ElisendaGascon Sep 23, 2022
415fa33
Remove how to use .NET section
ElisendaGascon Sep 26, 2022
6f40148
Change method name to use PascalCase
ElisendaGascon Oct 11, 2022
c0f2926
Change `IsEqualToOne` method ro return a `Task`
ElisendaGascon Oct 11, 2022
0e42b0c
Remove unnecessary code
ElisendaGascon Oct 11, 2022
96ebafb
Change return type of `ConsolePrint` to `Task`
ElisendaGascon Oct 11, 2022
345fc74
Change return type of `DivideAsync` to `Task`
ElisendaGascon Oct 11, 2022
52401d9
Remove extra variable `equalsZeroAction`
ElisendaGascon Oct 11, 2022
3c68767
Improve `ForEachUntilFalseAsync` description
ElisendaGascon Oct 11, 2022
0bc112b
Modify `ForEachUntilTrueAsync` description
ElisendaGascon Oct 11, 2022
b56159d
Remove unnecessary code
ElisendaGascon Oct 11, 2022
dcdcedf
Add markdown cell with explanation
ElisendaGascon Oct 13, 2022
208ea1a
Add explanation for the use of `ForEachFailEnd`
ElisendaGascon Oct 13, 2022
ced64b2
Remove extra variable allocation
ElisendaGascon Oct 13, 2022
3ccfdc6
Remove additional variable delcaration
ElisendaGascon Oct 13, 2022
2d7b63a
Remove all additional variable declarations
ElisendaGascon Oct 13, 2022
9c33e44
Change all async methods to use await
ElisendaGascon Oct 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions Corvus.Extensions.Samples/TraversalExtensionsSample.dib
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#!markdown
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

## Traversal Extensions - Sample Notebook

#!markdown

First, add a reference to the `Corvus.Extensions` NuGet package.

#!csharp

#r "nuget: Corvus.Extensions, 1.1.4"

using Corvus.Extensions;

#!markdown

### ForEachAsync()

#!markdown

Execute an async action for each item in the enumerable.

#!csharp

public async Task IsEqualToOne(int x)
{
bool result = x.Equals(1);
Console.WriteLine(result);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wrote this in Visual Studio, it would issue a warning because although you've declared this as async you don't seem to have used await, so this isn't really async.

I'd consider adding something like

await Task.Delay(TimeSpan.FromSeconds(0.5));

so that a) it is async, and b) it becomes reasonable clear that ForEachAsync waits for the Task returned for each item to complete before moving onto the next.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using await in all async methods now


#!csharp

var list = new List<int> { 1, 2, 3, 4, 5 };
await list.ForEachAsync<int>(IsEqualToOne)

#!markdown

The `ForEachAsync()` method is here for historical interest. It is now possible to write the following and get the same output:

#!csharp

foreach (int x in list)
{
await IsEqualToOne(x);
}

#!markdown

### ForEachAtIndex<T>()

#!markdown

Execute an action for each item in the enumerable with the index of the item in the enumerable.

#!csharp

static void ConsolePrint(string s, int i)
{
Console.WriteLine(i);
Console.WriteLine(s);
}

Action<string, int> printAction = ConsolePrint;

#!csharp

IEnumerable<string> list = new List<string> { "foo", "bar", "quux" };
list.ForEachAtIndex<string>(printAction);

#!csharp
jongeorge1 marked this conversation as resolved.
Show resolved Hide resolved

int i = 0;

foreach (string v in list)
{
printAction(v, i++);
}

#!markdown

### ForEachAtIndexAsync()

#!markdown

Execute an async action for each item in the enumerable, in turn, with the index of the item in the enumerable.

#!csharp

static async Task ConsolePrint(string s, int i)
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine(s);
Console.WriteLine(i);
}

#!csharp

IEnumerable<string> list = new List<string> { "foo", "bar", "quux" };
await list.ForEachAtIndexAsync<string>(ConsolePrint);

#!markdown

### ForEachFailEnd()

#!markdown

Execute an action for each item in the enumerable.

If any operation fails, then the enumeration is continued to the end when an Aggregate Exception is thrown containing the exceptions thrown by any failed operations.
jongeorge1 marked this conversation as resolved.
Show resolved Hide resolved

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

static void Divide(int x)
{
int result = 10/x;
Console.WriteLine(result);
}

Action<int> divideAction = Divide;

list.ForEachFailEnd<int>(divideAction)

#!markdown

### ForEachFailEndAsync()

#!markdown

Execute an async action for each item in the enumerable.

Returns a task which completes when the enumeration has completed.

If any operation fails, then the enumeration is continued to the end when an Aggregate Exception is thrown containing the exceptions thrown by any failed operations.

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

public async Task DivideAsync(int x)
{
int result = 10/x;
Console.WriteLine(result);
}
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

Func<int, Task> divideAction = DivideAsync;

await list.ForEachFailEndAsync<int>(divideAction)

#!markdown

### ForEachUntilFalse()

#!markdown

Execute an action for each item in the enumerable.

Returns false if the enumeration returned early, otherwise true.

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

static bool IsEqualToZero(int x)
{
bool result = x.Equals(0);
return result;
}

list.ForEachUntilFalse<int>(IsEqualToZero)

#!markdown

### ForEachUntilFalseAsync()

#!markdown

Execute an async action for each item in the enumerable.

Returns a task whose result is False if the enumeration returned early, otherwise returns true.

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

public async Task<bool> IsEqualToZero(int x)
{
bool result = x.Equals(0);
return result;
}
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

Func<int, Task<bool>> equalsZeroAction = IsEqualToZero;
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

await list.ForEachUntilFalseAsync<int>(equalsZeroAction)

#!markdown

### ForEachUntilTrue()

#!markdown

Execute an action for each item in the enumerable.

Returns true if the action terminated early, otherwise false.

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

static bool IsEqualToZero(int x)
{
bool result = x.Equals(0);
return result;
}

Func<int, bool> equalsZeroAction = IsEqualToZero;
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

list.ForEachUntilTrue<int>(equalsZeroAction)

#!markdown

### ForEachUntilTrueAsync()

#!markdown

Execute an async action for each item in the enumerable.

Returns a task whose result is True if the action terminated early, otherwise returns False.

#!csharp

IEnumerable<int> list = new List<int> { 1, 2, 8, 0, 5 };

public async Task<bool> IsEqualToZero(int x)
{
bool result = x.Equals(0);
return result;
}

Func<int, Task<bool>> equalsZeroAction = IsEqualToZero;
ElisendaGascon marked this conversation as resolved.
Show resolved Hide resolved

await list.ForEachUntilTrueAsync<int>(equalsZeroAction)