Our stacks and queues don’t exist in isolation - they have to interoperate with standard framework classes and other domain constructs to be useful. For example, it is extremely useful to have some factory methods to make it easier to construct stacks and queues from existing lists and sequences.
To begin, let’s implement a couple of extension methods - .EnqueueAll()
to add everything from a sequence onto a queue, and .ToQueue
to convert a sequence directly into a queue.
With that support, we can now write a static class with methods for making new Immutable Queues:
You’ll observe that we now have two ways to create a new queue from an existing sequence - by calling the static method ImmutableQueue.Create()
and by using the extension method .ToQueue()
. Depending on your context and preferences, you might prefer to drop one or the other.
There are similar methods that you can - and should - write for stacks, but I’ll leave those as an exercise for the reader. They’re very much the same as the ones shown here for queues, but with the wrinkle that items on a stack come off in the reverse order, so you’ll need to think through the semantics carefully before you start.
Comments
blog comments powered by Disqus