Interlude

Ink blotNot much is happening at the moment. Well, actually quite a bit is happening. It’s just that I find myself in a rather non-eventful interlude of some sort.

Last Friday (January, 29th) I had my last day at SMS Development & Support AS where I have been working for the last 1.5 years. It was my very first job as a full-time software developer and I learned a lot. Both about developing software and about myself as a developer. Good stuff!

Next up I will return to LifeStyleTV where I spent a year as a Media Missionary. This time I will be working there as a regular employee and help out with various things. Mainly I will be developing some internal software systems with some of the others there, but I will most likely also help out with the TV production. I am going down there the 11th of February and I am looking forward to get started. :)

What am I doing now, during this interlude? Not a lot. Very little. Extremely little. My computer is dead. I’m sick. My energy reserves are non-existent. I’m sleeping, eating, watching tv, reading… and that’s pretty much it… To be honest I am actually kind of happy I am sick now instead of earlier or later. Just hope I will be back to normal before Thursday… :|

Sleepy dog

Posted in Personal | Tagged , , | 2 Comments

How to check for duplicates

Say you have an IEnumerable<T> of some sort and you want to check if it contains any duplicates. How do you do that?

Terrible solution

I needed to do this a while ago and the first solution that hit me wasn’t exactly good. Linq has method called Distinct, which returns the distinct items in a sequence (weeds out duplicates). Using that to check if duplicates exists you can do something like this:

var hasDuplicates =
        subjects.Count() != subjects.Distinct().Count();

This is of course a really bad solution. So don’t use it! :P

Good solution

The much better solution is to use a HashSet<T>.

The HashSet<T> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

In addition to the fact that it cannot contain duplicate items, it also has a very nice add-method which returns false if the item is already in the set.

To check for duplicates using a hash set we can start out by assuming there is no duplicates and then add items to the hash set until we either run out of items, or we try to add an item that already exists. In the first case our assumption was right and in the second it turned out to be wrong.

var set = new HashSet<T>();

var hasDuplicates = false;

foreach (var s in subjects)
    if (!set.Add(s))
    {
        hasDuplicates = true;
        break;
    }

Good and handy solution

Since we don’t want to type in that stuff over and over again we want this snippet in a nice extension method. Code snippet coming up :)

using System.Collections.Generic;

namespace Geekality.Snippets
{
    public static class EnumerableExtensions
    {
        public static bool HasDuplicates<T>(this IEnumerable<T> subjects)
        {
            return HasDuplicates(subjects, EqualityComparer<T>.Default);
        }


        public static bool HasDuplicates<T>(this IEnumerable<T> subjects, IEqualityComparer<T> comparer)
        {
            if(subjects == null)
                throw new ArgumentNullException("subjects");

            if(comparer == null)
                throw new ArgumentNullException("comparer");

            var set = new HashSet<T>(comparer);

            foreach (var s in subjects)
                if (!set.Add(s))
                    return true;

            return false;
        }
    }
}

Simple and handy. You use it for example like this:

if(subjects.HasDuplicates())
{
    // Do something
}

Hope it can help someone, somewhere, below and/or over the rainbow :)

Let me know what you think. I’m always open for improvements and feedback 8)

Posted in Software Development | Tagged , , , | Leave a comment

Test-Driven Development: By Example

Test Driven Development By Example (Cover)I earlier wrote about the book, The Art of Unit Testing, which I finished a while ago. That book was very good and was focused on how to write good unit tests. It also mentioned Test-Driven Development, TDD, but not too much. The book I read next, which I finished a few days ago, was kind of the other way around. Pretty much only about TDD. And from the title, Test-Driven Development: By Example, that shouldn’t be much of a shocker :P

Read More »

Posted in Reviews, Software Development | Tagged , , , | Leave a comment

Technorati claim

Just trying to claim my blog at technorati… which seems to be more difficult than I had thought… here is my stupid claim token: AUSCVUHQEPXB . Now, read it!! You can do it technorati! Read it now! Come ooon! It’s right there! You know you want to!! :?

Posted in Technology | 4 Comments

How to test asynchronous events

The other day I had to test that an event was raised after some asynchronous work had been done. And since I currently am a total test newbie, this was a new thing for me. Say we have this simple shell of a class:

public class Worker
{
    public event EventHandler<eventargs> Done;

    public void Start()
    {
       ...
    }
}</eventargs>

Let’s just assume it does some work, and is supposed to raise the Done event when it is… well… done.

Read More »

Posted in Software Development | Tagged , , , , | Leave a comment

Generics and checking for null

When writing C#, in Visual Studio, using generics… have you ever tried checking for null? I have always found that a bit of a hassle.

Say we have this method which returns the subject if it is not null, and the result of a createNew() function if it is null.

public static T NewIfNull<t>(this T subject, Func</t><t> createNew)
{
    if (subject == null)
        return createNew();
    return subject;
}</t>

Not claiming this is a very useful method, but it was the simplest thing I could come up with :P Anyways, in Visual Studio you will now probably have a blue (is blue here at least) squiggly line under the equality operator. It states you are doing a Possible compare of value type with ‘null’, which of course is reasonable and correct. We could just ignore it and move on… but we don’t really like squiggly lines, do we? I sure don’t… so lets get rid of it.

Read More »

Posted in Software Development | Tagged , , | Leave a comment

Project Euler: Problem 25

The Fibonacci sequence is defined by the recurrence relation:
F_n = F_{n-1} + F_{n-2}, where F_1 = 1 and F_2 = 1.

Hence the first 12 terms will be:

  • F_1=1
  • F_2=1
  • F_{11}=89
  • F_{12}=144

The 12th term, F_{12}, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

Read More »

Posted in Project Euler, Software Development | Tagged , , , | Leave a comment
  • Twitter Facebook YouTube last.fm LinkedIn Google vimeo Technorati RSS feed
  • This would be meGreetings! I am Torleif Berger, 24 years old, a Seventh-Day Adventist and currently working as a software developer. Otherwise, not much to tell. Although I do juggle a bit...