Should I learn F#?

I love C# – it is so versatile and Linq is fantastic, but with F# being the latest and greatest I decided to watch Luca Bolognese’s “An Introduction to Microsoft F#” from PDC 2008.  Watching the sample unfold I couldn’t help wondering if I couldn’t do the same thing at least just as  cleanly in C# and Linq, with the aid of a couple of extension methods.

This is not to say that I found the presentation irrelevant – on the contrary it was very inspiring and putting functional programming paradigms into my head. I probably won’t put effort into becoming an F# programmer, but I am sure that Linq will be even more important in my future codestyle.

Here is the code that I came up with based on Luca’s sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace TestYahooData
{
    public static class ExtensionMethods
    {
        // Add the "missing" ForEach funtion to IEnumerable<T> like on List<T>
        // One should probably add a little null checking!
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (T item in source)
            {
                action(item);
            }
        }

        // Some double functions to maintain neat-ness
        public static double DivideBy(this double d, double divisor) { return d / divisor; }
        public static double Squared(this double d) { return d * d; }
        public static double Sqrt(this double d) { return Math.Sqrt(d); }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var YahooStockData = WebTextFile()
                .ToList() // Lousy -> better performance by avoiding multiple webrequests
                .Skip(1)
                .Select(e => e.Split(','))
                .Select(e => new { date = DateTime.Parse(e[0]), amount = double.Parse(e[5]) })
                ;

            // Testdata
            //YahooStockData = (new[] {
            //    new {dato = DateTime.Now, antal = 2.0},
            //    new {dato = DateTime.Now, antal = 4.0},
            //    new {dato = DateTime.Now, antal = 6.0},
            //    new {dato = DateTime.Now, antal = 8.0}
            //});

            Console.WriteLine(YahooStockData.Count());

            var avg = YahooStockData
                .Average(e => e.amount);
            Console.WriteLine(avg);

            var std = YahooStockData
                .Sum(d => (d.amount - avg)
                .Squared())
                .DivideBy(YahooStockData.Count())
                .Sqrt();
            Console.WriteLine(std);

            Console.ReadLine();
        }

        static IEnumerable<string> WebTextFile()
        {
            string url = "http://ichart.finance.yahoo.com/table.csv?s=msft";

            using (var stream = WebRequest.Create(url).GetResponse().GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                        yield return reader.ReadLine();
                }
            }
        }
    }
}

Advertisements