A couple of questions left

Now, I have some time to write about a couple of questions I have received about F#, and their answers.

1. To get a specific element of a tuple :

If we do not want to create variables for the elements that we are not interested in of a tuple, we can use “_” for all other elements, i.e.:

let drinks= (Coke, 0.45, “Coca Cola Zero”)
let _, _, valBrand= drinks

which is interpreted into the compiler as we see in the FS Interactive window:

val drinks : string * float * string = (“Coke”, 0.45, “Coca Cola Zero”)
val valBrand : string = “Coca Cola Zero”

2. “namespace” keywords stays as is, and we can have either a module or a type [i.e. C# class] under a namespace. The example would be :
File1: Drink.fs

namespace Vesta.Beverage
module Drink=
let drinks= (Coke, 0.45, “Coca Cola Zero”)
let _, _, valBrand= drinks

File2: BeverageCalculator.fs [placed under the Drink.fs if they are in the same project.]

namespace Vesta.Calculator
open Vesta.Beverage.Drink
module BeverageCalculator=
let drink= valBrand

So, we can use everything from the first namespace,
Happy F# days!

Comments from my First Talk at ldnUG about F#

First of all, thanks all who had their valuable time spent on my talk about F# at EMC Conchango.
Thanks to Michelle for beers, to Zi for finding an HDMI cable at the last minute, to Liam for lovely chocolate cakes, to Argos for support, and to everyone for coming…

It was my first talk in England, and as it was a public talk, I was not sure about the content, examples. Even if I am coming from a trainer background, and I know that different level of people had attended my .NET Courses, it was quite hard to decide the depth of the subject, and to deliver the message I want to deliver. Every decision has a drawback, going simple can bore people, whereas going into complicated scenarios can leave some people out.
Rather than taking the challenging option :
– I decided to name the talk as “Intro”, so I thought meeting the expectations would be easier.
– Rather than going theoritical and covering more aspects of F#, I wanted to go over a simple demo, and share the feeling of writing in F# with VS2010.

Thanks God, it was not a disaster, and I got comments to work on, and improve, which is always good.

1. A complete sample:
My presentation was missing a complete sample, as my demos were quite small sized examples. When the seminar finishes, the audience would have felt more satisfied, if I had started and finished an F# project.
I had thought about this beforehand, but was not sure if I could have a project which is “simple” enough so that the audience can follow, “clean” enough so that I do not have to hacks during demo, and “complex” enough so that the audience would take home and work at home. I admit it is hard, but not achivable.

2. The balance of the slides/demo
Interestingly I got a feedback telling that I was better when I was coding during the presentation, and this was quite unusual for user group events; and I got feedbacks telling that I was disconnected from the audience while I was coding, so my mood/excitement was negatively affecting my touch to keyboard, and I was seen more confident while I was doing the talk and answering the questions.
Making everyone happy is impossible of course, but I can take both feedbacks as sensible.  After seeing 50 people in the room, [full room, with people sitting on the edge of the window], my confidence has jumped out of the window, instead of myself. I reckon, the less perfection I am, the more communicator I will be.
I love coding, so coding during the whole session would be my pleasure, but there is the other feedback comes into the screen:

3. How well you know your keyboard/touchpad/mouse?
The audience does nothing but sits and watch your coding experience. Little tweaks, multiple corrections to your mistakes does make your audience confused and breaks their focus. Bringing a keyboard/mouse that you daily use is the suggestion and I will try to take this one. Working more on my new laptop is the other option, and is a good alternative obviously, but it would never replace my keyboard used 8-10 hours a day.
What happens is that they may either start to interest into something different or start to sleeping. Worse than that, they may start talking to each other. Talking more about what you do and doing less changes at a time, does keep everyone back on board.

4. Communicating contact details
As the questions can be tricky during the session, you may need to give the answers later. But if you have not included your contact details at the end of the presentation, there is a high probability that, you will forget to tell “I will post the answers to my blog as soon as possible” as I forgot.
If you have anything you want to add, please do!

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.

static void Main(string [] args)
{
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.

let lessThanFive 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.

class Program
{
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:

let dir folder=
Directory.GetFiles folder
|> Array.iter(fun x-> printfn “%s” x)
let result= dir @”C:\Program Files (x86)\Microsoft F#\v4.0″