lördag 9 september 2017

Livemap24, shows public transports on a map in "realtime"

The site Livemap24 shows millions of individually tracked public transport units on a navigatable map. It seems to be real time, but when reading discussions about it, it seems like they don't have enough data to be able to show exact positions for all of them. Some probably just are best guessed from a time table.

Cool idea that can be better when more open data becomes available!

Individually tracked bus in Årsta, Stockholm.

Is this heart rate signature normal? (Or is it a hardware / software / biological bug?)

During the current running season I bought myself a Garmin Forerunner 235 gps watch, which have given me more insights into my running. Before I didn't get statistics of my cadence or heart rate (and was a bit worried that my phone would take damage if it started to rain when out for a run).

Now that I have gathered heart rate statistics for a while, I see a recurring pattern in almost all of my heart rate diagrams. In the beginning of the run the heart rate is rather stable at a value, but suddenly there is a threshold and the heart rate jumps to a stable significantly higher pace.

I think it makes perfectly sense that the heart rate increases during running, but I find that steep rise a bit odd and therefore wonder what could cause it. I suspect that my wrist becoming sweaty after a while can play a role in this, but still, I think that wouldn't show up as that steep in the diagram.

This season I mostly have been running 5 km runs and haven't been doing any warm-up before starting running. 

Here are other diagrams.

This diagram looks more what I would suspect most of the diagrams would look like, but its kind is rare for me.

A seven km run with a jump from 120 to 170+ at around 3 km.

 A five km run with a jump from 140 to 170 at around 3,5 km.

A five km run with a jump from 120- to 160+ at around 1 km.

So, it this anything that looks familiar to you? In that case, any idea what causes the steep rise?

Want to learn? 3 months Pluralsight for free. If you haven't tried it, I really advise you to because I like it a lot!

I have taken a few courses on Pluralsight to learn about WPF, ASP.NET MVC and Linq. My experience is that it is a really fast way to get into something. The courses are nicely separated into different modules and sections, so if you feel you already know some of the content it is easy to navigate to the parts that you need to know more about.

And I prefer to learn something by having someone speaking about it than to just read about it.

To get your free months, you have to sign-up on Visual Studio in the cloud, but that's also free. Just follow the instructions linked below.


I'm on to learn some basics about Angular 2, just to get a grip on what it's about.

In case you use the offer, hope you'll have a nice learning experience!

tisdag 5 september 2017

"ToLookup" to the rescue when "GroupBy" or "ToDictionary" isn't quite right for the job

More than once I've been writing code using LINQs GroupBy and ToDictionary methods with a feeling that the code didn't become as clear as I wanted it to be. I did some web searching, hoping that there might exist a third party library with a smart solution, but what I found though, was that I didn't even have to install another library, LINQ already contained a method for it!

I'll try to show you how ToLookup can be useful with a phony example.


Suppose a group of smurfs take turn rolling a die and want us to print the result on the screen, like this:
 Smurfs that rolled a 1  
  - Grouchy Smurf  
  - Greedy Smurf  
 Smurfs that rolled a 2  
  - Smurfette  
 Smurfs that rolled a 3  
  - Papa Smurf  
  - Hefty Smurf  
 Smurfs that rolled a 4  
 Smurfs that rolled a 5  
  - Brainy Smurf  
 Smurfs that rolled a 6  
  - Clumsy Smurf  

For this I use a class to store each result in:
 class DieRollResult  
   public string Player { get; set; }  
   public int Value { get; set; }  

And a list to store all the results of a round in:
 var resultList = new List<DieRollResult>()  
   new DieRollResult{ Player= "Papa Smurf", Value = 3},  
   new DieRollResult{ Player= "Smurfette", Value = 2},  
   new DieRollResult{ Player= "Hefty Smurf", Value = 3},  
   new DieRollResult{ Player= "Brainy Smurf", Value = 5},  
   new DieRollResult{ Player= "Grouchy Smurf", Value = 1},  
   new DieRollResult{ Player= "Clumsy Smurf", Value = 6},  
   new DieRollResult{ Player= "Greedy Smurf", Value = 1},  

A solution using GroupBy

Notice the usage of SingleOrDefault to find the right group, it isn't possible to find it by using an index. 
Notice also that using this result set no smurf rolled a 4, and because empty groups like this are allowed a null check has to be done before iterating through the group to avoid a NullReferenceException.

A solution using GroupBy combined with ToDictionary

This time, notice the rather verbose expression to assign the variable resultGroups.
Notice also that this time, the group can be fetched using an index, but we still have to check if the dictionary really contains that index key to avoid a KeyNotFoundException.

A solution using ToLookup

So, finally, by using the ToLookup method, the assignment of the variable resultGroups is as short and clear as in the GroupBy example.
It is also possible to use an index to get the right group to iterate through. And since the ILookup object just returns an empty IEnumerable when it is indexed with a key that it doesn't contain, there is no need to check for null or if the key exists like we had to do in the other examples!

This is why I like the ToLookup method, it raises the signal-to-noise ratio a tiny bit, but sometimes it is just that small improvement that can make the code clearer. 

On the downside though, the method seems to be used so seldom that I think few programmers have seen it, so using it might make the code break the "Principle of least surprise".