Thursday, December 13, 2007

Biking the wrong way

Today I drove the U-Haul truck from Port Townsend to Sequim to drop it off. My plan was to bring my bicycle along, bike from the drop-off to the bus stop, and catch the bus back home. This bus only runs about every 3 hours, so it was important to time things correctly. I failed.

First, I didn't have internet access working in the new house, so I couldn't look up bus info, and couldn't remember it well.

Second, I wasn't paying attention to the clock.

Third, I rode my bike on the wrong path. Here's what I could have done:


View Larger Map

(Assume I cut across that sharp left turn that Google Maps won't let me do, because it thinks this is for a car.)

But I turned left too early, and meandered around for a while. Here's what I did instead:


View Larger Map

That's 7.5 miles. I'm pretty impressed with myself, considering how long it has been since I was in shape, and how, with the move & everything, the last couple months have been little exercise and a gain of 10 pounds.

And then, after the bus ride, I rode the bike up the big hill to the house.

And then, I put the babies in the stroller, and walked to the grocery store to get dinner for the family.

I'm feeling proud of myself, and very hopefull for my future exercise.

Saturday, December 08, 2007

An attempt at an immutable Queue

Eric said he will write about an immutable queue implementation. I really like the way the immutable stack turned out, essentially using references between Stack<> objects to implement a linked list, but I couldn't figure out a similarly elegant way to do the same thing for Queue. Maybe people who are more clever will come up with something better, but I just had each Queue<> object contain a list of references to elements.

I wrote once in C# 2.0, using an array to store the elements. The code would be cleaner if I used a List<>, but then I would be harder to verify immutability.

class Queue<T> : IQueue<T>

{

public static readonly IQueue<T> Empty = new Queue<T>(new T[] { });


 

readonly T[] elements;


 

Queue(T[] elements)

{

this.elements = elements;

}

public bool IsEmpty { get { return this.elements.Length == 0; } }

public T Peek() { return this.elements[0]; }

public IQueue<T> Remove()

{

T[] newElements = new T[this.elements.Length - 1];

Array.Copy(this.elements, 1, newElements, 0, newElements.Length);

return new Queue<T>(newElements);

}

public IQueue<T> Add(T value)

{

T[] newElements = new T[this.elements.Length + 1];

Array.Copy(this.elements, newElements, this.elements.Length);

newElements[newElements.Length - 1] = value;

return new Queue<T>(newElements);

}

public IEnumerator<T> GetEnumerator()

{

for (IQueue<T> Queue = this; !Queue.IsEmpty; Queue = Queue.Remove())

yield return Queue.Peek();

}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }

}

Then I rewrote using C# 3.0, taking advantage of the rich support for sequences. The code is much simpler, but I think that's basically because the IEnumerable extension methods do pretty much what I did in my first attempt. But I'll take it. J

class Queue2<T> : IQueue<T>

{

readonly IEnumerable<T> elements;


 

public static IQueue<T> Empty = new Queue2<T>(new T[] { });


 

Queue2(IEnumerable<T> elements)

{

this.elements = elements;

}


 

IQueue<T> IQueue<T>.Add(T value)

{

return new Queue2<T>(this.elements.Concat(new T[] { value }));

}


 

IQueue<T> IQueue<T>.Remove()

{

return new Queue2<T>(this.elements.Skip(1));

}


 

T IQueue<T>.Peek()

{

return this.elements.First();

}


 

bool IQueue<T>.IsEmpty

{

get

{

return this.elements.Count() == 0;

}

}


 

IEnumerator<T> IEnumerable<T>.GetEnumerator()

{

return this.elements.GetEnumerator();

}


 

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return this.elements.GetEnumerator();

}

}


 


 


 


 

 
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.