Don't Break The Build Software development articles and tutorials

10Jan/120

In Response To: Iterating Over A Tuple

Today I saw Tombatron's blog post on Iterating Over A Tuple about ways to iterate over a Tuple. While I must say it is a very bad idea to do this (if you want to store your data in something iterable, then use something other than a Tuple), I found a very interesting challenge in this.

Tombatron implements Extension Methods and converts the Tuple into a List which you can iterate from there. The drawback to that method is that Tombatron had to account for every tuple (so Tuple, Tuple, etc) which, while it works, just felt like it could be improved on. So I got to work.

At first I wanted to inherit from the Tuple class but then ran into issues being able to inherit from all of them as there is not one base class. From there I decided that it would be better to start from IEnumerable and go from there. Since this is more or less a proof of concept for myself, I decided to use a List for storage and then just wrap the list's enumerator with my own.


The next task was figuring out how to support a varying number of arguments. That was achieved by using the params with dynamic to allow for heterogeneous types. On top of that, I created an indexer so that it could still be accessed like the normal Tuple (and without me having to create several functions).

Overall, I had fun creating this little bit of code and know that it will not be used in production (at least I pray it wouldn't be). Here is the code so you can see what I did:

/*
 * Written by Jonathan Hartwell
 * http://www.dontbreakthebuild.com
 * 1/10/2012
 * 
 * 
 */
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var t1 = IterableTuple.Create(1.5, 12, "string", 15, 69);
 
            foreach (var it in t1)
            {
                Console.WriteLine(it);
            }
            Console.ReadLine();
        }
    }
 
 
    public class IterableTuple : IEnumerable
    {
        private List<dynamic> storage;
        int position = -1;
        public IterableTuple(params dynamic[] args) 
        { 
            storage = new List<dynamic>();
            for (int i = 0; i < args.Length; i++)
            {
                storage.Add(args[i]);
            }
        }
        public static IterableTuple Create(params dynamic[] args)
        {
            return new IterableTuple(args);
        }
 
        public dynamic this[int i]
        {
            get
            {
                if (i < storage.Count)
                {
                    return storage[i];
                }
                else
                {
                    throw new IndexOutOfRangeException();
                }
            }
        }
 
        public IEnumerator GetEnumerator()
        {
            return storage.GetEnumerator();
        }
 
    }
 
}


Tagged as: Leave a comment
Comments (0) Trackbacks (0)

No comments yet.





Leave a comment

No trackbacks yet.