Skip to content

Instantly share code, notes, and snippets.

@tkokof
Created November 2, 2018 12:46
Show Gist options
  • Save tkokof/bc168c12a51f50b28a36356467686218 to your computer and use it in GitHub Desktop.
Save tkokof/bc168c12a51f50b28a36356467686218 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
public interface ITuple
{
int Length
{
get;
}
object this[int index]
{
get;
}
}
public class Tuple<T> : ITuple
{
public int Length
{
get
{
return 1;
}
}
public object this[int index]
{
get
{
Debug.Assert(index == 0);
return m_item;
}
}
public Tuple(T item)
{
m_item = item;
}
T m_item;
}
public class Tuple<T, TRest> : ITuple where TRest : ITuple
{
public int Length
{
get
{
return 1 + m_rest.Length;
}
}
public object this[int index]
{
get
{
if (index == 0)
{
return m_item;
}
else
{
return m_rest[index - 1];
}
}
}
public Tuple(T item, TRest rest)
{
m_item = item;
m_rest = rest;
}
T m_item;
TRest m_rest;
}
public static class Tuple
{
public static Tuple<T> Create<T>(T item)
{
return new Tuple<T>(item);
}
public static Tuple<T1, Tuple<T2>> Create<T1, T2>(T1 item1, T2 item2)
{
return new Tuple<T1, Tuple<T2>>(item1, new Tuple<T2>(item2));
}
public static Tuple<T1, Tuple<T2, Tuple<T3>>> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
{
return new Tuple<T1, Tuple<T2, Tuple<T3>>>(item1, new Tuple<T2, Tuple<T3>>(item2, new Tuple<T3>(item3)));
}
public static Tuple<T1, Tuple<T2, Tuple<T3, Tuple<T4>>>> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4)
{
return new Tuple<T1, Tuple<T2, Tuple<T3, Tuple<T4>>>>(item1, new Tuple<T2, Tuple<T3, Tuple<T4>>>(item2, new Tuple<T3, Tuple<T4>>(item3, new Tuple<T4>(item4))));
}
// more Create interfaces here ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment