Friday, July 18, 2008

Functional Programming in C#

Functional programming style can save a lot of time.
Here are somme samples from Microsoft site itself.
C# tend to include some F# features.
Give it a look, it will change your life as a C# developper ;-)

The following topics are covered

Closures
Currying
Filter
Fold
Iterators
Lazy Evaluation
LINQ
Lists (Immutable and Recursive)
List Continuations
Maps/Map2
Memoization
Monads
Operators (Forward, Reverse, etc)
Recursion
Unfolding and Generators

http://code.msdn.microsoft.com/FunctionalCSharp

Wednesday, July 16, 2008

List Comprehension in C# with LINQ - part 2

In this example, I tried to figure how to use
a static method to filter an array

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

namespace LINQArray
{
class Program
{
public static bool filter(string s)
{
bool ret = false;
if (s.StartsWith("A"))
{
ret = true;
}
return ret;
}

static void Main(string[] args)
{
string[] countries = { "Britain", "France", "Croatia", "Argentina", "Australia", "Mexico", "Finland",
"Spain", "Italy", "Greece" };
var thecountries = from c in countries
where filter(c)
select c;
foreach (var c in thecountries)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
}

List Comprehension in C# with LINQ

List comprehensions from Python (from Haskell) miss me a lot in C#
Happily with LINQ I dont miss then anymore :

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

namespace ListComprehension
{
class Program
{
static void Main(string[] args)
{
string[] countries = { "Britain", "France", "Croatia", "Argentina", "Australia", "Mexico", "Finland",
"Spain", "Italy", "Greece" };

string[] favcountries = { "Britain", "Argentina", "Greece", "Brazil" };

var thecountries = from c in countries
where favcountries.Contains(c)
select c;

foreach (var c in thecountries)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
}

LINQ - .NET

LINQ stands for "Language Integrated QUery"
This really great feature can drastically ease SQL request.
Here is an example :

var products = from p in db.Products
where p.Category.Categoryname == "Beverages"
select p;

Here is the corresponding SQL statement :

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
LEFT OUTER JOIN [dbo].[Categories] AS [t1] ON [t1].[CategoryID] = [t0].[CategoryID]
WHERE [t1].[CategoryName] = 'Beverages'

Choose your camp :-)

Tuesday, July 15, 2008

C# - Learning Events

Here is my simple attempt on implementing events in C#
Any comment would be appreciate.

using System;
namespace TutorialEvents
{
    delegate void PersonHandler(string information);

    class Person
    
        protected string name = "Anonymous";
        public static event PersonHandler PersonEvt;

        public void Process(string name)
        {
            OnPersonEvt(name);
        }

        protected void OnPersonEvt(string name)
        {
            string oldname;
            if (PersonEvt != null)
            {
                oldname = this.name;
                this.name = name;
                PersonEvt(String.Format("OldName : {0} - NewName : {1}", oldname, this.name));
            }
        }
    }

    class ControlNameChange
    {
        static void myPersonEvt(string message)
        {
            Console.WriteLine(message);
        }

        public void Subscribe()
        {
            Person.PersonEvt += new PersonHandler(myPersonEvt);
        }
    }

    class Test
    {
        public static void Main(String[] args)
        {
            Person p = new Person();
            ControlNameChange controlNameChange = new ControlNameChange();
            controlNameChange.Subscribe();
            p.Process("Henri");
            p.Process("Marcel");
            Console.ReadLine();
        }
    }
}