Piped arguments#
public class Program
{
static int Main(string[] args) => AppRunner.Run(args);
public static AppRunner AppRunner => new AppRunner<Program>();
public void Range(IConsole console, int start, int count, int sleep = 0)
{
foreach (var i in Enumerable.Range(start, count))
{
console.WriteLine(i);
if (sleep > 0)
{
Thread.Sleep(sleep);
}
}
}
public void Sum(IConsole console, IEnumerable<int> values)
{
int total = 0;
foreach (var value in values)
{
console.WriteLine(total += value);
}
}
}
Here we've converted the arguments for Sum into an IEnumerable
We could have used List
Very few console frameworks make it this easy to write streaming console tools.
Let's see it in action:
~
$ dotnet linq.dll Range 1 4 10000 | dotnet linq.dll Sum
1
3
6
10
After outputtting a value, Range sleeps for 10 seconds. We know Sum is streaming because it immediatly outputs the new sum as soon as it receives a value and waits for the next value.