browse by category or date

F# is a functional programming language that recently officially added to Visual Studio.NET supported languages. To me it reminds me alot to Python because of the indentation rules. If you want to play around with this language, you could either use:

  1. Visual Studio 2010
  2. Visual F# 2011 CTP
  3. Visual Studio 2012 Developer Preview
  4. Your browser

What made me attracted so much to F#? It was caused by this informative, interesting and humorous session by Luca Bolognese. And yes, since he’s an Italian, his introduction to programming in F# is an offer that I can’t refuse! 😀

In his talk, Luca highlighted three most important keywords in F#. They are:

  1. let. This keyword is similar, but different to var in C# or Dim in VB. But let is actually more on binding one object/value to a symbol. This symbol will be an immutable object, which means the its value/object can’t be change during its life-cycle.
  2. |>. This operator is similar to pipe in MS-DOS.
  3. fun. This keyword is used in creating lambda function.

Luca also highlighted that F# gives you the facility to immediately concentrate on the core solution of the problem. Because in F#, Luca said, we can program the solution of a problem just like the way we would solve it in our brain.

Anyway, let’s get dirty with simple example in F#:

//bind x to an integer
let x = 10;

//create a list of integers
let data = [1;2;3;4]

//define a square function
let sqr x = x * x 

//Luca notes that this method is something that we will do 
//in C#
let sumOfSquaresI nums =
    //"let mutable" will create a variable instead of binding to a symbol
    let mutable acc = 0
    for x in nums do
        //assign value to acc
        acc <- acc + sqr x
    //return the value
    acc

//This is the same function, but in a recursive function
let rec sumOfSquaresF nums =
    //match is a branching mechanism plus binding
    match nums with
    | [] -> 0
    //h::t will match to a list 
    //and bind symbol h to the first element of the list
    //and bind symbol t as the rest of the elements in the list
    | h::t -> sqr h + sumOfSquaresF t

//This is the F# way of doing Sum of Squares
let sumOfSquares nums =
    nums
    //Operator |> is similar to a DOS pipeline
    //Seq.map will apply the function sqr to each elements in the list
    |> Seq.map sqr
    //By now the list we have is a list of squares, e.g. [1;4;9;16]
    //Seq.sum will sum every elements in the list and return an integer
    |> Seq.sum

From above code, I hope we can quickly spot the difference between programming in imperative way (sumOfSquaresI) vs. the F#-way (sumOfSquares). Clearly, the F#-way gives us not many room to make mistake, and if you think of it, it is indeed how we would solve it in our brain (first, square every element in the list, then sum it up).

Another plus point is parallelization. Parallelization is very much working out of the box in F#. We can easily parallelize the function sumOfSquares by calling the parallel-version of the method.

let sumOfSquares nums =
    nums
    |> Seq.pmap sqr
    |> Seq.psum

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

It feels great when I realized that I’ve just found out a new way of doing things. But it feels kinda sucks when that “new way” is apparently has been around for quite sometime. It did made me feel bad about myself a little. Because that means either I have not been looking hard enough, or I didn’t even bother to find out because I can use my old method to achieve the same result (a.k.a. Comfort Zone Leaving Avoidance Syndrome). 😀

Since this problem is related to MS SQL Database, we need to imagine that we have this condition:

  1. There are two tables, Categories (key: CategoryID) and Posts (has a CategoryID field)
  2. Posts to Categories is in 1-to-1 relationship

Now we are required to keep track of how many Posts under a particular Category. For that purpose, an integer field called PostCount is added to Categories table. In order to maintain the correct PostCount value, triggers would need to be added in Posts table. These triggers will be invoked whenever a Post is created, updated and deleted. But before that, we need to run an update query to initialize the PostCount value. And this update query is where I learned my “no-longer-new way”.

If you asked me to do this last week, I would definitely insisted that this is unworkable in SQL. I would then quickly write a small program to:

  1. Retrieve all the Categories
  2. Iterate each Category, and count how many Posts with this Category
  3. Update Category

Fortunately, the request insisted that it must be done in SQL. Thus, it forced me to trial and error, as well Googling for it. As I found out today after many trials, above steps could be done in SQL. Here’s how:

Update CCC
Set CCC.PostCount = PPP.Counted
From Categories CCC
Inner Join (
   Select CategoryID, Count(*) as Counted
   From Posts
   Group By CategoryID
) PPP
on CCC.CategoryID=PPP.CategoryID;

The joy of finding out this method was significantly reduced when I found out that this guy blogged about it almost 5 years ago! 😀

Cheers!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Have you seen the Angels Gate series in Channel News Asia? If not you can watch it here ONLINE. It’s a reality show about entrepreneurs trying to obtain funding from the investors (aka, the Angels). After watching a few episodes, I believe we can all agree that apart from having a solid business model, the ability to pitch our business is a something that an entrepreneur must have.

A few days ago I saw a posting by Oren Klaff listing down his pitching strategy. I think it’s relevant, especially if you are harboring an idea to start your own business. And I am listing it down here hoping that it will be somehow permanently engraved in my brain 🙂

  1. Recognize your own value so you’ll have confidence

    An analyst was asking Oren lots of nitpicky questions, but Oren viewed himself as the prize of the deal so he had the confidence to turn the questioning back on her and ask how she added value to the deal.

    Take Action: Think of yourself as a prize that investors have to compete for, and don’t feel you need to beg to get funding.

  2. Start with big ideas to set the context for your pitch

    Oren was raising capital for people who built protective ecosystems in Peru, and he started the pitch by saying that to save humanity you have to solve big problems like climate change, pollution, and species loss, and that species loss stands out because it’s irreversible.

    Take Action: Before you mention your product, talk about the big ideas that motivated you to create your product and point out the problem you want to solve.

  3. Compare solutions to the problem to show that your product is the best one

    Oren recommends describing a generic solution, listing your competitors’ solutions, and then introducing your product and explaining how it solves the problem.

    Take Action: After you’ve highlighted the problem you want to solve, compare your product with other potential solutions and show that you’ve found the right way to address the problem.

  4. Reveal your project’s stage upfront to save time

    Oren says that one entrepreneur should have disclosed his project’s stage at the beginning of his presentation, but he didn’t so he had to face questions like “Are you profitable?” after going through an hour-long pitch.

    Take Action: Start your pitch by talking about project details like how much money you’ve raised, how many employees you have, and how much revenue you earn.

  5. Negotiate with multiple investors so you won’t have to accept unfavorable terms

    Oren recommends meeting with several investors, narrowing the field down to three who are seriously interested, and negotiating with those three simultaneously, because if one insists on terms you don’t like you can sign a deal with one of the others.

    Take Action: Negotiate with at least three investors at the same time, and don’t talk to one investor exclusively before you close a deal.

  6. Set a time limit to show you aren’t needy

    Oren says that entrepreneurs who pitch to a hedge fund intake committee should limit themselves to an hour, but some of them talk for three or four hours and it looks like they have nothing else to do.

    Take Action: Establish a time constraint at the beginning of a meeting with investors, and give your pitch in 20 minutes.

  7. Create and resolve tension to keep investors listening

    Oren suggests that an entrepreneur could say, “I’m not sure we’re going to work well together because you might want us to over-focus on products” to grab investors’ attention and then say, “But having some guys who are so focused on products and engineering combined with our business development skills might be a good thing!”

    Take Action: Create tension by bringing up a possible conflict with investors, then resolve the tension by giving a reason the conflict won’t occur.

  8. Save interesting points for later so you can use them to win investors’ attention

    When Oren pitches for a genetic testing company, he mentions that it tests athletes late in the pitch because that’s a novel point and it brings back investors’ attention.

    Take Action: Think of some new and surprising things your business does, and talk about them at the middle or end of your pitch if your audience seems to be losing interest.

With slight modification, we can use this techniques for other purpose other than pitching to investor. If you’re an employee in a company, you can use these techniques to make your next PowerPoint presentation more appalling to the audience.

Good luck!



About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: