public
Last active

Usage for monadic Future in C#

  • Download Gist
ExampleFutureUsage.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
using System;
using System.Threading;
using XSharpx;
using System.Collections;
using System.Net;
 
namespace TestProj
{
class MainClass
{
public static void Main (string[] args)
{
 
 
Console.WriteLine ("Total Page size for search engines");
var c = new CountdownEvent(1);
 
var totalPageSize =
from googlePage in GetPageFuture ("http://www.google.com")
from bingPage in GetPageFuture ("http://www.bing.com")
from ddgPage in GetPageFuture ("http://www.duckduckgo.com")
select googlePage.ToCharArray().Length + bingPage.ToCharArray().Length + ddgPage.ToCharArray().Length;
 
totalPageSize.Map (s => {
Console.WriteLine ("Total size is = " + s);
return c.Signal();
 
});
 
Console.WriteLine ("At the end of main");
c.Wait();
 
}
 
public static Future<String> GetPageFuture (String str)
{
return Future.Create (()=>GetPage (str));
}
 
public static String GetPage (String str)
{
var ret = "";
using (var client = new WebClient()) {
ret = client.DownloadString (str);
}
return ret;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.