Let's start with something simple: an immutable stack.
Now, immediately I hear the objection: a stack is by its very nature something that changes. A stack is an abstract data type with the interface
void Push(T t);
T Pop();
bool IsEmpty { get; }
You push stuff onto it, you pop stuff off of it, it changes. How can it be immutable?
Every time you need to make a data structure immutable, you use basically the same trick: an operation which "changes" the data structure does so by constructing a new data structure. The original data structure stays the same.
How can that possibly be efficient? Surely we'll be allocating memory all over the place! Well, actually, in this case, no. An immutable stack is every bit as efficient as a mutable stack. Even better: in some cases, it can be considerably more efficient, as we'll see.
Let's start by defining an interface for our immutable structure. While we're at it, we'll fix a problem with the stack ADT above, namely that you cannot interrogate the stack without changing it. And we'll make the stack enumerable just for the heck of it:
public interface IStack<T> : IEnumerable<T>
{
IStack<T> Push(T value);
IStack<T> Pop();
T Peek();
bool IsEmpty { get; }
}
Pushing and popping give you back an entirely new stack, and Peek lets you look at the top of the stack without popping it.
Now let's think about constructing one of these things here. Clearly if we have an existing stack we can construct a new one by pushing or popping it. But we have to start somewhere. Since every empty stack is the same, it seems sensible to have a singleton empty stack.
Read full article from Immutability in C# Part Two: A Simple Immutable Stack - Fabulous Adventures In Coding - Site Home - MSDN Blogs
No comments:
Post a Comment