Skip to content

Instantly share code, notes, and snippets.

@robfe
Created November 29, 2010 10:17
Show Gist options
  • Save robfe/719791 to your computer and use it in GitHub Desktop.
Save robfe/719791 to your computer and use it in GitHub Desktop.
T4 template to generate some useful N-Tuple overloads of common System.Reactive extension methods
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension="Generated.cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
using System;
using System.Linq;
using System.Reactive;
namespace <#=CallContext.GetData("NamespaceHint")#>
{
public static partial class ObservableExtensions
{
<#
for(int i = 1; i < 8; i++)
{
#>
public static Tuple<<#=Types(i)#>, TOther> Append<<#=Types(i)#>, TOther>(this <#=Container(i)#> s, TOther o)
{
return new Tuple<<#=Types(i)#>, TOther>(<#=Values(i)#>, o);
}
<#
}
for(int i = 1; i < 8; i++)
{
#>
public static IObservable<Tuple<<#=Types(i)#>, TOther>> CombineLatest<<#=Types(i)#>, TOther>(this IObservable<<#=Container(i)#>> source, IObservable<TOther> other)
{
return source.CombineLatest(other, Append);
}
<#
}
for(int i = 1; i < 8; i++)
{
#>
public static IObservable<Tuple<<#=Types(i)#>, TOther>> Zip<<#=Types(i)#>, TOther>(this IObservable<<#=Container(i)#>> source, IObservable<TOther> other)
{
return source.Zip(other, Append);
}
<#
}
#>
}
}
<#+
public string Container(int i)
{
return i == 1 ? "T1" : "Tuple<"+Expand(i, "T{0}")+">";
}
public string Values(int i)
{
return i == 1 ? "s" : Expand(i, "s.Item{0}");
}
public string Types(int length)
{
return Expand(length, "T{0}");
}
public string Expand(int length, string format)
{
return string.Join(", ", Enumerable.Range(1, length).Select(x=>string.Format(format, x)));
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment