fsharpforfunandprofit.com icon indicating copy to clipboard operation
fsharpforfunandprofit.com copied to clipboard

Rewrite the C# vs F# page

Open swlaschin opened this issue 7 years ago • 1 comments

Re #5, update the page to be more OO vs FP rather than C# vs F#

swlaschin avatar Jan 25 '18 11:01 swlaschin

That page is helpful, but does not use shortcuts in C# examples that are used F# examples.

For example, in F# the following example is given:

let sites = ["http://www.bing.com";
             "http://www.google.com";
             "http://www.yahoo.com"]

and in C# the parallel is:

var sites = new List<string> {
        "http://www.bing.com",
        "http://www.google.com",
        "http://www.yahoo.com"};

To make them closer to equivalent, the F# could be written to specify type:

let sites : string [] = ["http://www.bing.com";
             "http://www.google.com";
             "http://www.yahoo.com"]

Or the C# could be written more tersely:

var sites = {
        "http://www.bing.com",
        "http://www.google.com",
        "http://www.yahoo.com"};

Additionally, a lot of extra vertical space is introduced unnecessarily that can be removed for a closer 1:1 comparison (even the inner pair of curly braces could be removed, but that seems a bit much)...

class WebPageDownloader
{
    public TResult FetchUrl<TResult>(string url, Func<string, StreamReader, TResult> callback)
    {
        using (var resp = WebRequest.Create(url).GetResponse())
        using (var stream = resp.GetResponseStream())
        using (var reader = new StreamReader(stream))
        {
          return callback(url, reader);
        }
    }
}

...and there's no reason for this to have been on 3 lines (and could be made even more terse with recent C# updates)...

Console.WriteLine("Downloaded {0}. First 1000 is {1}", url, html1000);

GaTechThomas avatar Apr 19 '19 19:04 GaTechThomas