I haven't bothered with a raw pipe, but I have tried making all references into the Maybe monad:
public static class ReferenceExtensions
{
public static TOut IfNotNull<TIn, TOut>(this TIn v, Func<TIn, TOut> f)
where TIn : class
where TOut: class
{
if (v == null)
return null;
return f(v);
}
}
Then suppose you have an object model that lets you lookup a RecordCompany by name, and then lookup a Band within that RecordCompany, a Member of the Band, and any of these might return null, so this might throw a NullReferenceException:
var pixiesDrummer = Music.GetCompany("4ad.com")
.GetBand("Pixes")
.GetMember("David");
We can fix that:
var pixiesDrummer = Music.GetCompany("4ad.com")
.IfNotNull(rc => rc.GetBand("Pixes"))
.IfNotNull(band => band.GetMember("David"));
Hey presto, if any of those transitions return null, pixiesDrummer will be null.
Wouldn't it be great if we could do extension methods that are operator overloads?
public static TOut operator| <TIn, TOut>(TIn v, Func<TIn, TOut> f)
Then I could pipe together my transition lambdas like this:
var pixiesDrummer = Music.GetCompany("4ad.com")
| rc => rc.GetBand("Pixes")
| band => band.GetMember("David");
Also wouldn't it be great if System.Void was defined as a type and Action was really just Func<..., Void>?
Update: I blogged a little about the theory behind this.
Update 2: An alternative answer to the original question, which is roughly "How would you express the F# pipe-forward operator in C#?"
Pipe-forward is:
let (|>) x f = f x
In other words, it lets you write a function and its first argument in the opposite order: argument followed by function. It's just a syntactic helper that assists with readability, allowing you to make use of infix notation with any function.
This is exactly what extension methods are for in C#. Without them, we would have to write:
var n = Enumerable.Select(numbers, m => m * 2);
With them, we can write:
var n = numbers.Select(m => m * 2);
(Ignore the fact that they also let us omit the class name - that's a bonus but could also be made available for non-extension methods as it is in Java).
So C# already solves the same problem in a different way.