Convert program to 2020: replace foreach loop with map, filter and friends

Vladimir Panteleev thecybershadow.lists at gmail.com
Tue Mar 31 05:23:13 UTC 2020


On Tuesday, 31 March 2020 at 04:00:28 UTC, User wrote:
> I'd like to convert the following program to 2020 standards 
> (i.e, replace the foreach block with a one-line code). I've 
> tried much and I failed.

Here is how I'd do it.

Because the program downloads and then reads the local file 
unconditionally, I replaced it with byLine. This allows it to 
process the data as it is being downloaded, without first waiting 
to download everything.

import std;

void main()
{
	immutable URL = 
r"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
	immutable country = "Poland";

	URL
		.byLine
		.map!(line => line.splitter(","))
		.enumerate
		.filter!(t => t.index == 0 || t.value.dropOne.front == country)
		.map!(t => t.value)
		.take(2)
		.each!writeln;
}



More information about the Digitalmars-d-learn mailing list