Skip to content

Instantly share code, notes, and snippets.

@robfe
Last active December 21, 2015 00:09
Show Gist options
  • Select an option

  • Save robfe/6217984 to your computer and use it in GitHub Desktop.

Select an option

Save robfe/6217984 to your computer and use it in GitHub Desktop.
Create as many n-tuples as you like
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ output extension=".Designer.cs" #>
using System.Collections.Generic;
namespace <#=CallContext.GetData("NamespaceHint")#>
{
<#
const int TotalTuples = 16;
for (int i = 1; i < TotalTuples; i++)
{
var numbers = Enumerable.Range(1, i);
#>
public static partial class Tuple
{
public static Tuple<<#=string.Join(", ", numbers.Select(x=>"T"+x)) #>> Create<<#=string.Join(", ", numbers.Select(x=>"T"+x)) #>>(<#=string.Join(", ", numbers.Select(x=>"T"+x+" item"+x)) #>)
{
return new Tuple<<#=string.Join(", ", numbers.Select(x=>"T"+x)) #>>(<#=string.Join(", ", numbers.Select(x=>"item"+x))#>);
}
}
public class Tuple<<#=string.Join(", ", numbers.Select(x=>"T"+x)) #>>
{
public Tuple(<#=string.Join(", ", numbers.Select(x=>"T"+x+" item"+x)) #>)
{
<#
foreach (var n in numbers)
{
#>
Item<#= n #> = item<#= n #>;
<#
}
#>
}
<#
foreach (var n in numbers)
{
#>
public T<#= n #> Item<#= n #> { get; private set; }
<#
}
#>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != this.GetType())
{
return false;
}
var o = (Tuple<<#=string.Join(", ", numbers.Select(x=>"T"+x)) #>>)obj;
<#
foreach (var n in numbers)
{
#>
if(!EqualityComparer<T<#=n#>>.Default.Equals(this.Item<#=n#>, o.Item<#=n#>)){ return false; }
<#
}
#>
return true;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = 0;
<#
foreach (var n in numbers)
{
#>
hashCode = (hashCode*397) ^ EqualityComparer<T<#=n#>>.Default.GetHashCode(Item<#=n#>);
<#
}
#>
return hashCode;
}
}
}
<#
}
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment