Why should we use functional programming: a simple example

Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: hr line 5 and body in Entity, line: 6 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: body line 3 and html in Entity, line: 7 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): Premature end of data in tag html line 1 in Entity, line: 8 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: Invalid argument supplied for foreach() in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 160

Good evening,

I’m just taking the time to add more content on functional programming. When I talk about this paradigm with programmers who haven’t had the time to work with it, they always tell me that the code is difficult to understand and that it would be difficult to maintain. I would like to show a simple example which shows that this statement is wrong.

Because I would agree that if you work in an environment where nobody works in F# it is not very likely that you will switch programming technology, I will use C# for my example. Indeed, C# supports some functional programming features as discussed in this book:

Functional Programming in C#: Classic Programming Techniques for Modern Projects (Wrox Programmer to Programmer)

I will take a simple example to demonstrate why functional programming can actually be much simpler to read and help developers avoid making mistakes. Let’s assume you have a list of integers and that you want to get the sum of the squares of the elements of the list.

In imperative programming (the paradigm used normally in C#) you would proceed as follows:

int[] myList = new int[] {1,2,3};

double res=0;
for (int i=0; i<3; i++)

return res;

As you can see, it takes about 6 lines (with clear formatting) because you have to setup a result variable, and the you have to make sure that this variable is implemented by the square of each number. You also need to see that you don’t make any mistake in the definition of the loop as it depends on the length of the list.

Now, let’s see how you can do exactly the same thing using functional programming:

int[] myList = new int[] {1,2,3};

return myList.Select(x=>Math.Pow(x,2)).Sum();

As a matter of fact, you can see that the “algorithm” is much shorter there is no problem considering the length of the list or setting up a return variable. I also believe that the code is easier to read, because you do not have to tell the program everything it has to do, you just explain the logic and the actual implementation happens in the background.

That’s it for today,

See you next time!

CFA Level I: that was it.

Hi everybody,

I finally took the Level I exam of the CFA curriculum yesterday. I take the opportunity to express my feelings about how my studies went and what my feelings were about the actual exam. As a matter of fact, I cannot disclose any of the specific questions I (and the other candidates) encountered in the real exams, because of the strict CFA exam policies.

The Level I curriculum

I believe the main thing I will remember about the curriculum is its diversity and its broad coverage of the financial world. Let me just restate the different sections of the curriculum:

  • Ethics
  • Quantitative Methods
  • Economics
  • Accounting
  • Corporate Finance
  • Portfolio Management
  • Equity Investments
  • Fixed Income Investments
  • Derivatives
  • Alternative Investments

I liked the fact that I think that you will find some challenges even in the section you think you already master. For example, I knew most of the subjects presented in Quantitative Methods, but still a few things were presented in a different way, a way that I didn’t face during more mathematical studies. These new approaches lead to surprising questions; but I think I mastered them quite quickly.

I really learnt a lot in terms of broad markets, you have an explanation of the different types of asset classes and it’s not only about valuation, it’s also about how it works, what kind of instruments should be used given a specific situation, so that’s great and it really helped me reading some articles and during my fund selection duties.

Economics was a difficult section for me. As a matter of fact, the program is huge compared to the number of questions that is allocated to this topic. Hence, as I don’t have a degree in Economics (and these economics are not approached really mathematically), it was really all about damage limitation.

For accounting, I believe some questions are really approachable. EPS and DuPont analysis help you get at least a few questions right without hesitation; there is not debate or understanding really involved as you will get it right if you know the criteria. However, I believe there are some questions that you can hardly answer if you are not doing accounting in your daily job.

The exam

About the exam itself, I have mixed feeling about how it went, and I’m not really sure of the outcome, but I’m never confident about an exam so we’ll see. As I mentioned I cannot discuss about the content, however I can say that the organization was, as expected, very strict. I saw people not being able to take the exam because they didn’t have a valid passport. I even had to show an additional piece of ID because I was much thinner on my passport picture than I am now! But if you’re just reading your e-mail and the exam policy on the CFA Institute website, you’ll be fine really.

That’s it for today, I’m really happy this is over and I will be back with more content on quantitative finance soon!