F# vs C# Samples
After my talk at ldnUG yesterday, I got nice suggestions from Zi Makki, and here is the first one: comparison between F# and C#, especially using LINQ. This subject had been quite popular once in a while, now it is probably my time to focus on this subject. My initial samples are quite common: getting numbers less than 5 from a list, and getting files from a specified directory.
{
List list= new List { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LessThanFive(list);
}
private static void LessThanFive(List list)
{
string y = list
.Where(x => x <= 5)
.Select(z => z.ToString())
.Aggregate(” “, (seed, n) => seed + n);
}
If we want to write this in F#, List.filter will be enough for filter operation, and the List.iter will go through the list, and do any operation/function we define for each item in the list.
list
|> List.filter ((>=) 5) >>
|> List.iter (printfn “%d”)
let z = lessThanFive [1..9]
The second example if for printing the folder names
With the help of the extension method explained at stackoverflow, here is a better C# code to get the files of a specified path.
{
static void Main(string[] args)
{
Dir(@”C:\Program Files (x86)\Microsoft F#\v4.0″);
Console.ReadLine();
}
private static void Dir(string
{
Directory.GetFiles(path)
.ForEachWithIndex(
(item, idx) => Console.WriteLine(“{0}: {1}”, item, idx));
}
}
public static class ForEachExtensions
{
public static void ForEachWithIndex(this IEnumerable enumerable, Action <T,int> handler)
{
int idx = 0;
foreach (T item in enumerable)
handler(item, idx++);
}
}
And the F# version would be quite simple, as Array.iter is enough to process the array resulting from GetFiles:
Directory.GetFiles folder
|> Array.iter(fun x-> printfn “%s” x)
let result= dir @”C:\Program Files (x86)\Microsoft F#\v4.0″
(fun x -> printfn “%d” x)
may be replaced with:
(printfn “%d”)
and (fun x -> x) 5)
Controlflow
May 14, 2010 at 11:40 am
Hi,
Probably I am missing something about your comment due to a typo error.
List.iter looks for a function to evaluate and “fun” is the keyword to start to define a function.
And what we want to achieve is to print after the function evaluates the value, i.e. if bigger than 5 print.
With your example, the line would be like this?
List.iter (printfn “%d” (fun x -> x> 5))
continuousdevelopment
May 15, 2010 at 8:03 am
[...] Ebru Cucen’s F# vs C# Samples [...]
Rick Minerich's Development Wonderland : F# Discoveries This Week 05/14/2010
May 14, 2010 at 9:07 pm
[...] This post was mentioned on Twitter by dmohl. dmohl said: "F# vs C# Samples" by @ebrucucen http://bit.ly/atveI8 #fsharp [...]
Tweets that mention F# vs C# Samples « Updates from a continuous developer -- Topsy.com
May 15, 2010 at 1:16 pm
Try:
let lessThanFive = List.filter ((>=) 5) >> List.iter (printfn “%d”)
lessThanFive [1..9]
and
let dir = Directory.GetFiles >> Array.iter (printfn “%s”)
dir “c:\\”
I find it a bit more succinct and readable. For the first one, I’d have used Seq instead of List, since that’s more generalisable and you’re only going forward in any case.
Yusuf Motara
May 17, 2010 at 8:11 am