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

Taskex sample notebook #194

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
126 changes: 126 additions & 0 deletions Corvus.Extensions.Samples/TaskExSample.dib
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!markdown

## TaskEx - Sample notebook

#!markdown

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

#!csharp

#r "nuget: Corvus.Extensions, 1.1.4"

#!markdown

Add using statement for `Corvus.Extensions` namespace

#!csharp

using System.IO;
using Corvus.Extensions;
using Corvus.Extensions.Tasks;

#!markdown

### WhenAllMany()

This method flattens the outputs from `Task.WhenAll`
Lmooney25 marked this conversation as resolved.
Show resolved Hide resolved

#!markdown

#### Examples

The following example uses `WhenAllMany` to asynchronously download all the OPML feeds in a set of feed collections and return a flattened collection of the results.
jongeorge1 marked this conversation as resolved.
Show resolved Hide resolved

#!csharp

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

#!csharp

public record Feed(string Name, string Author, string Format, Uri Url);

#!csharp

public class FeedDownloadResult
{
public string Content { get; set; }

public HttpStatusCode StatusCode { get; set; }
Lmooney25 marked this conversation as resolved.
Show resolved Hide resolved
}

#!csharp

public class FeedCollection
{
public FeedCollection(Feed[] feeds)
{
this.Feeds = feeds;
}

public IEnumerable<Feed> Feeds { get; private set; }

public string Name { get; set; }

public async Task<IEnumerable<FeedDownloadResult>> GetAllFeedsAsync()
{
List<Task<FeedDownloadResult>> feedTasks = new List<Task<FeedDownloadResult>>();
foreach (Feed feed in this.Feeds)
{
Task<FeedDownloadResult> feedDownloadResult = GetFeedAsync(feed);
feedTasks.Add(feedDownloadResult);
}
FeedDownloadResult[] results = await Task.WhenAll(feedTasks).ConfigureAwait(false);
return results;
}

private async Task<FeedDownloadResult> GetFeedAsync(Feed feed)
{
var request = new HttpRequestMessage(HttpMethod.Get, feed.Url);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);

return new FeedDownloadResult
{
Content = response.IsSuccessStatusCode ? await response.Content.ReadAsStringAsync() : null,
StatusCode = response.StatusCode
};
}

}

#!csharp

FeedCollection[] feedCollections =
{
new FeedCollection
(
new Feed[]
{
new Feed("Chris Risner's blog", "Chris Risner", "RSS", new Uri("https://feeds.feedburner.com/Chrisrisnercom")),
new Feed("Joseph Jude's blog", "Joseph Jude", "RSS", new Uri("https://jjude.com/feed.xml"))
}
),

new FeedCollection
(
new Feed[]
{
new Feed("Marius Sandbu's blog", "Marius Sandbu", "RSS", new Uri("https://msandbu.wordpress.com/feed/")),
new Feed("Jakob Ehn's blog", "Jakob Ehn", "RSS", new Uri("https://blog.ehn.nu/feed/"))
}
)
};

#!csharp

Func<FeedCollection, Task<IEnumerable<FeedDownloadResult>>> mapper = (feedCollection) =>
{
return feedCollection.GetAllFeedsAsync();
};
IList<FeedDownloadResult> result = await TaskEx.WhenAllMany<FeedCollection, FeedDownloadResult>(feedCollections, mapper);
return result[0].Content;
65 changes: 65 additions & 0 deletions Corvus.Extensions.Samples/TaskExSampleExperiments.dib
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!markdown
Lmooney25 marked this conversation as resolved.
Show resolved Hide resolved

## TaskEx - Sample notebook

#!markdown

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

#!csharp

#r "nuget: Corvus.Extensions, 1.1.4"

#!markdown

Add using statement for `Corvus.Extensions` namespace

#!csharp

using System.IO;
using Corvus.Extensions;

#!markdown

### WhenAllMany()

#!markdown

#### Examples

#!csharp

int[] numbers = {1,2,3,4};
string[] letters = {"A", "B", "C"};

numbers.SelectMany(n => letters, (n, l) => $"number: {n}, letter: {l}")

#!csharp

string[][] input =
{
new[]{"Hello", "World"},
new[]{"Cat", "Dog", "Bird"}
};

input.SelectMany(a => a)

#!csharp

foreach (var a in input)
{
foreach (var item in a)
{
Console.WriteLine(item);
}
}

#!csharp

foreach (var a in numbers)
{
foreach (var item in letters)
{
Console.WriteLine($"number: {a}, letter: {item}");
}
}