Last active
December 17, 2015 15:29
-
-
Save vmarquez/5632123 to your computer and use it in GitHub Desktop.
Usage for monadic Future in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
//all calls happening concurrently | |
var googPF = GetPageFuture ("http://www.google.com"); | |
var bingPF = GetPageFuture ("http://www.bing.com"); | |
var ddgPF = GetPageFuture ("http://www.duckduckgo.com"); | |
//now we sequence the results in an easy to read fashion wihtout callbacks | |
var totalPageSize = | |
from googlePage in googPF | |
from bingPage in bingPF | |
from ddgPage in ddgPF | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some sample (possible) implementations.
https://github.com/vmarquez/xsharpx/tree/master/src/XSharpx/Promise.cs
https://github.com/vmarquez/xsharpx/blob/master/src/XSharpx/Future.cs