Fortsätt till huvudinnehåll

Inlägg

Visar inlägg från juni, 2017

Take a look at the beautiful F# code

I have taken a special interest in parallel and asynchronous data processing in F#. The absolute majority of the really hard to fix bugs that I have come across during the last years all have one thing in common, thread safety. Or rather, the lack of thereof. It is very easy to write a class in C#, Java, or C++ that is not thread safe. And if you try to make it thread safe it quickly can become quite complex and it is easy to forget to put a lock on a variable or make some subtle mistake that makes the lock useless (locking on a reference to an object that you later make a copy of, which makes locking the old reference useless, is a not so uncommon mistake that can be really hard to spot). Another thing about thread safety is that it is really hard to test for. All your tests might run perfectly fine, but then when you go into production, things starts crashing in really strange ways. F#, and other functional languages, addresses these problems and makes it easy to write code tha

Another go at functional programming

I have been wanting to learn functional programming for real for a while now. I must say, it has been tough. Being used to procedural and object oriented programming languages like C, C#, and Java, I am used to being able to grasp new programming languages quite fast. But functional languagues, that's a completely different story. In pure functional programming there are no variables, no states, none of the things you are used to in an object oriented language. It is like starting over from scratch. Let me give you an example, assume you want to calculate the sum of all even numbers between 0 and 100 squared. How would you go about implementing that? Well, I would define a variable, sum, that keeps track of the sum so far. Then I would create a for loop, for (int i = 2; i <= 100; i += 2), in which I add i * i to sum. In functional programming however, you should avoid mutable values and loops. So what to do? Well here is one example on how you can do it in F#: [0..100]