One of the nicer spots

Posted a month and 3 week(s) ago

No alternative to graphics, sorry

For a long time i have been wanting to do some posts on things i like or dislike about C# and the whole .net thingy, and since i'm using it everyday at work there is a remote possibility that i will hit on something interesting.

The first thing i will cover is the yield keyword. For python developers this is all old hat, but for C# developers it is a great improvement to the language.

Basically it rids you of implementing all sorts of "crazy" iterators to supply a generic interface to collections of internal data that you wish to expose.

Normally you try to write generic code, and make your objects as loosely coupled as makes sense, this introduces a problem when you are working with internal collections of objects.

For instance, consider a Department containing a collection of Student(s) - in the first implementation this could be implemented as a simple vector of Student(s) (a List in .net), and you might expose the internal data structure by providing a Students property like the following:

   IList<Student> Students
   {
      get
      {
         return m_students;
      }
   }

Ok, so far so good, the users of the Department class can now see which students belong to a particular Department, but the users of the class can still modify the collection, they believe they can index the list by integer index, and so on, and so on, the IList<> interface is _very_ broad. You could expose it as an ICollection<> but that would still expose the collection. What you really should do, is have the Students property return an IEnumerable<Student> instance.

This will make the internal way that a Department class represents students completely independent of the way users of the class access them. Luckily List<Student> already exposes the IEnumerable<> interface - so, easy!

   IEnumerable<Student> Students
   {
      get
      {
         return m_students;
      }
   }

What happens now is that someone comes along and says - hey! Finding out whether or not a student is in a particular Department just by traversing a list is too slow! So you decide to go with something like a Dictionary mapping from student id (SSN or something similar) to the Student object. But you still want users to be able to, simply, traverse the list of students. With a Dictionary that is not so easy, even though a Dictionary exposes the IEnumerable<> it does so for a special Dictionary.KeyValuePair type - which is no good for our purpose. This is where yield will help us for the first time - check out how i implement an IEnumerable<Student> class in 5 lines:

   IEnumerable<Student> Students
   {
      get
      {
         foreach ( KeyValuePair<int, Student> pair in m_students )
         {
            yield return pair.Value;
         }
      }
   }

Isn't that just almost pythonic'ly beautiful? Isn't that just the simplest way possible to implement that particular task?

--

An even nicer example.

Where it will really save your ass is if you need to traverse a tree structure sequentially, then you just yield your way on to beatiful code - no more note keeping while trying to traverse the tree correctly. Just implement something simple like the following:

class TreeNode
{
    List<TreeNode> m_children;
    ...
    IEnumerable<TreeNode> AllChildren
    {
        get
        {
            foreach ( TreeNode child in m_children )
            {
                yield return child;
                foreach ( TreeNode subChild in child.AllChildren )
                {
                    yield return subChild;
                }
            }
        }
    }
}

This is pretty much as simple as you are going to get, of course it would be nice with support for a call to yield to automatically yield every member of another IEnumerable<> object, such that the innermost foreach could be replaced with a simple yield child.AllChildren;. That would be _really_ awesome. Does anybody know where to report enhancements (and bugs for that matter) to mirosoft?

You still have the same cross-thread issues as with List<> and Dictionary<>'s implementations of IEnumerator<> but hey, you can't have it all!

Cheers!


-- A microsoft fanboy!

P.S. I haven't even scrached the surface of what the yield keyword can do for you - there is no requirement that you can only expose data throught yield, how about an implementation of python's xrange(from,to,increment) in C#:

   IEnumerable<int> Range(int _from, int _to, int _increment)
   {
      for ( int i = _from; i < _to, i += _increment ) return yield i;
   }

Strangely pleasing! (bonus points for writing a generic version)

Or perhaps a way to filter the output:

   IEnumerable<Student> Seniors
   {
      get
      {
         foreach ( Student student in m_students )
         {
            if ( student.TimeOnCampus > 3 ) yield return student;
         }
      }
   }

Which of course is horribly specific, why not generic' it up a bit:

   IEnumerable<Student> FilterStudents ( Predicate<Student> _filter )
   {
      foreach ( Student student in m_students )
      {
         if ( _filter(student) ) yield return student;
      }
   }

Not hard to see why lambda expressions is part of the new C#, and LINQ also seems closely related to this.

It's not python, but it's is pretty close for a statically linked language without template (as in C++) support.


Hey, i actually said something positive about something microsoft did! I _must_ be getting old, maybe i should pick up smoking again.

0 comments

A little bit of python

Posted 4 months and 0 week(s) ago

I finally found a bit of time to hack a bit on the evolution spambayes plugin, and there is now a version 0.1.6 ready, which works with Evolution 2.12.

Hopefully this will hopefully make Tom Arnold and Jim Rorie happy, or at least give them a reason to try _yet again_ to see if they can get it to work.

There is a new package in my DebianRepository for those fortunate enough to be running Debian (should also work on Ubuntu).

I am not reporting anything on company wise so far, other than: We're working on it!

0 comments

The big jump

Posted 6 months and 2 week(s) ago

Ok, this time i actually have something to tell.

I've quit my job!

Oh yeah, scary stuff!

I have a former colleague who have been working on a project that he has been wanting do to for a long time, actually he has worked for a long time on the project and he is now ready to make the jump to "hey look at me! i'm a company".

Well since he is going to start a company he, of course, need employees, and guess what, i am the first one!

So from January the first (preferably the second to avoid too many hangovers) i am going full-time with Sequanto.

We will start with focusing on creating the best damn system testing tool on the face of this planet (and maybe others), we are both tool-guys so there should plenty of opportunity for making a terrific tool.

This might mean that this blog will be focusing more on testing, but i am sure that you can live with that, _any_ focus is probably a good thing at this point.

Oh, and merry christmas to those celebrating it.

(And if you need some testing software for embedded devices or otherwise, don't hesitate to write me)

0 comments

Second pair of gloves

Posted 6 months and 4 week(s) ago

No alternative to graphics, sorry

As a little "in between" i am posting some of the code if have lying around, the first one is some code from my time at the university, more specifically from the game programming days. I am not sure that i have posted this before, which really is a shame, the only thing i could find about it is this pre C++ then Java post.

The game is called Temporal Wars and is a turn-based strategy game, all graphics and sounds were Made by Nerds(tm), needless to say i did some sound effects - my graphics skills have already been ridiculed on this site before.

The code (plus graphics and sounds) are available from here: http://dev.infonet.dk/~rto/bzr/temporalwars/ using Bazaar.

There is the start of some debian packaging in there, but it doesn't work at the moment (patches please?), and there is no win32 setup at the moment.

The next piece of code is a little elisp to make rcirc, the build-in IRC client in Emacs, work better with the modern desktop. More specifically it will notify you using the notify framework (windows users will call this "popup balloon) when someone writes a message to you (or including your username really).

If you have not tried the rcirc client yet - you really should!. It does just what is supposed, and you can go a long way even if the only IRC command you know is /join #channel.

(add-hook 'rcirc-print-hooks 'my-rcirc-print-hook)
(defun my-rcirc-print-hook (process sender response target text)
  (when (and (string-match (rcirc-nick process) text)
	     (not (string= (rcirc-nick process) sender))
	     (not (string= (rcirc-server-name process) sender)))
    (start-process "notify" nil "notify-send" (concat "rcirc: " sender) text)))

On Debian you need to install the libnotify-bin package to get the notify-send tool.


Oh, and if someone has my leather gloves that i left down at the pub, i would really like them back.

Gah! Second pair of nice black leather gloves i've lost down there. Now i just don't buy leather gloves anymore!

3 comments

Bugfix release for evolution-plugin-spambayes

Posted 10 months and 4 week(s) ago

So the release was not really _that_ tested yet, so a new release is available (0.1.4) containing a minor but crucial fix.

I'm of to work, see ya!

8 comments
Show all/old blog entries