Skip to content

Instantly share code, notes, and snippets.

@andersonimes
Created May 10, 2012 23:06
Show Gist options
  • Save andersonimes/2656478 to your computer and use it in GitHub Desktop.
Save andersonimes/2656478 to your computer and use it in GitHub Desktop.
[Rx]Extensions for tagging an event stream. Useful for when you need to combine two observables and can't use anonymous types
public static class ObservableMixins
{
public static IObservable<ObjectTag<T, V>> Tag<T, V>(this IObservable<V> sequenceToTag, T tag)
{
return sequenceToTag.Tag(_ => tag);
}
public static IObservable<ObjectTag<T, V>> Tag<T, V>(this IObservable<V> sequenceToTag, Func<V, T> tagFunction)
{
return sequenceToTag.Select(v => new ObjectTag<T, V>(tagFunction(v), v));
}
}
public class ObjectTag<T, J>
{
public T Tag { get; private set; }
public J Value { get; private set; }
public ObjectTag(T tag, J val)
{
Tag = tag;
Value = val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment