You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Comparing the use of foreach loop without lambdas and the use of a pipeline
(such as ....map(..->...).reduce(..->...)) with lambdas and the resulting
sequential versus concurrent behavior in different programming languages
Java, Js, C# and Kotlin.
asyncfunctionfetchAndSum(...urls){letsum=0for(consturlofurls){console.log(`FETCHING from ${url}`)constres=awaitfetch(url)constbody=awaitres.text()console.log(`=======> from ${url}`)sum+=body.length}returnsum}
asyncfunctionfetchAndSumλ(...urls){returnurls.map(async(url,i)=>{console.log(`FETCHING from ${url}`)constresp=awaitfetch(url)constbody=awaitresp.text()constlength=body.lengthconsole.log(`=======> from ${urls[i]}`)returnlength}).reduce(async(prev,curr)=>awaitprev+awaitcurr)}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831
C#
staticasyncTask<int>FetchAndSum(string[]urls){intsum=0;using(HttpClienthttpClient=newHttpClient()){foreach(varurlinurls){Console.WriteLine($"FETCHING from {url}");varbody=awaithttpClient.GetStringAsync(url);Console.WriteLine($"=======> from {url}");sum+=body.Length;}}returnsum;}
staticasyncTask<int>FetchAndSumλ(string[]urls){using(HttpClienthttpClient=newHttpClient()){returnawaiturls.Select(async url =>{Console.WriteLine($"FETCHING from {url}");varbody=awaithttpClient.GetStringAsync(url);Console.WriteLine($"=======> from {url}");returnbody.Length;}).Aggregate(async(prev,curr)=>awaitprev+awaitcurr);}}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831
Kotlin
suspendfunfetchAndSum(varargurls:String): Int {
var sum =0for (url in urls) {
println("FETCHING from $url")
val resp = httpClient
.sendAsync(request(url), BodyHandlers.ofString())
.await()
println("=======> from $url")
sum = sum + resp.body().length
}
return sum
}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831