If only I were

Building Great Software

Archive for December 2008

An Iowa City Blogger

with one comment

I met Bill Sorenson at the Iowa Code Camp recently and figured out that he has been blogging on tech topics for quite some time. Go check out his blog here.

Written by Chris Sutton

December 9, 2008 at 10:04 pm

Func<T> based Generic List Initializers

with 6 comments

A couple of weeks ago I was writing some code to initialize List<T> but my technique was very verbose and it seemed to be a distraction from what I was trying to do. Plus it seemed that every one else was writing about getting their func on.

So I looked back at some code that Nick Parker had written:

   1: public class Builder
   2: {
   3:     public static TType Create<TType>(Action<TType> actionOnTType) where TType : new()
   4:     {
   5:         var item = new TType();
   6:         actionOnTType(item);
   7:         return item;
   8:     }
   9: }

And tweaked it to initialize a list:

   1: public class Builder
   2: {
   3:     public static List<TType> CreateList<TType>(long size, Func<TType> initExpression)
   4:     {
   5:         var items = new List<TType>();
   6:         for (long i = 0; i < size; i++)
   7:         {
   8:             TType item = initExpression();
   9:             items.Add(item);
  10:         }
  11:
  12:         return items;
  13:     }
  14: }

Now I can initialize a list like this:

   1: List<Door> doors = Builder.CreateList(100, () => new Door {IsOpen = false});

Just for the record the Func initialization above could handle much more complex scenarios if needed. My usage is very simple.

Another variation on this is to create an extension method that does the same initialization like this:

   1: public static void Init<TType>(this IList<TType> values, int size, Func<TType> initExpression)
   2: {
   3:     for (int i = 0; i < size; i++)
   4:     {
   5:         TType item = initExpression();
   6:         values.Add(item);
   7:     }
   8: }

And here is how you would use the extension method version:

   1: var doors = new List<Door>();
   2: doors.Init(10, () => new Door());

Thanks to Nick Parker for some suggestions as I was working on this.

Something I would like to figure out is how to do this based on IEnumerable instead of IList. If you have ideas regarding implementing this on IEnumerable please add them as comments.

Also, if you want to see where I was using this, pull down the subversion source here: http://subversion.assembla.com/svn/solon-tools/trunk/Puzzles

kick it on DotNetKicks.com

Written by Chris Sutton

December 6, 2008 at 1:19 pm

Posted in Learning, Technology

Tagged with , ,

Learning Python

leave a comment »

I’ve almost learned Python several times in the recent past, but now I’m committing to learn it. Some of my learning resources are The Python Tutorial and Dive Into Python. What are other good online resources?

I’ve installed Python 3 on my machine and also have IronPython setup so I can keep up with Microsoft’s implementation. Another goal with IronPython is to be able to us it in an ASP.NET project.

Written by Chris Sutton

December 6, 2008 at 12:07 am

Posted in Learning, Technology

You can stop putting Change History in source, it’s 2008

with 4 comments

CHANGE HISTORY
========================================================
AUTHOR            LAST MODIFIED  DESCRIPTION
========================================================
v-someone        2008-08-21       Initial Version

It’s official, we are at the end of 2008 and there are lots of good source control tools out there(some of the best are free).

You don’t need to keep putting change history comments in your source if you are using source control. If you add comments when you commit your code this will be handled for you.

Written by Chris Sutton

December 5, 2008 at 10:37 am

Posted in Technology, Thoughts

Follow

Get every new post delivered to your Inbox.