TransformManyBlock C# with Example



TransformManyBlock C# with Example

(SelectMany, 1-m: The results of this mapping are “flattened”, just like LINQ ’s SelectMany) 
TransformManyBlock is very similar to TransformBlock. 
The key difference is that whereas a TransformBlock produces one and only one output for each 
input, TransformManyBlock produces any number (zero or more) outputs for each input. As with 
ActionBlock and TransformBlock, this processing may be specified using delegates, both for 
synchronous and asynchronous processing. 
A Func is used for synchronous, and a Func> is used for 
asynchronous. As with both ActionBlock and TransformBlock, TransformManyBlock defaults to sequential processing, but may be configured otherwise. 
The mapping delegate retuns a collection of items, which are inserted individually into the output buffer. 
 

Asynchronous Web Crawler 
var downloader = new TransformManyBlock(async url => 
{ 
Console.WriteLine(“Downloading “ + url); 
try 
{ 
return ParseLinks(await DownloadContents(url)); 
} 
catch{} 
return Enumerable.Empty(); 
}); 
downloader.LinkTo(downloader); 
Expanding an Enumerable Into Its Constituent Elements 
var expanded = new TransformManyBlock(array => array); 
Filtering by going from 1 to 0 or 1 elements 
public IPropagatorBlock CreateFilteredBuffer(Predicate filter) 
{ 
return new TransformManyBlock(item => 
filter(item) ? new [] { item } : Enumerable.Empty()); 
} 
Introduction to TPL Dataflow by Stephen Toub 

0 Comment's

Comment Form

Submit Comment