Introducing AWright18.PipeTo

I write a lot of C# code. I’m guessing most people who might read this also use C# more than most other languages. I have been writing more F# in my free time and in doing so I have noticed there is one operator that I try to use more than any other. That is the forward pipe operator, |>. Because of this I have created a C# library named AWright18.PipeTo to emulate the F# forward pipe operator. You can find the project on GitHub, and on Nuget.

Forward Piping in F#

In F# the , |>, operator is used to take the result of a previous function call and pass it to a new function. You can pass just about any value to the a function using the forward pipe operator.

A simple example in F#.

"Hello" |> (fun x -> x + " World")

This example passes the string “Hello” to the function that prints the value of x concatenated with ” World”;

The forward pipe operators are often strung together in a series to perform many functions in a row.

"Hello"
|> (fun x -> x + " World")     // "Hello World"
|> (fun x -> x + " from")      // "Hello World from"
|> (fun x -> x + " AWright18") // "Hello World from AWright18"

I always thought it would be nice to have this kind of feature in C#. It might save some typing, and allow us to express more simply a chain of function calls. The way this would work in C# is in a “fluid interface” style.

Forward Piping in C# with AWright18.PipeTo

If you want to try out forward piping in C# first you need to install the AWright18.PipeTo package into your C# project. You can find instructions on how to do that here.

Once you have the package installed add a using AWright18.PipeTo to your class, and then start piping away.

Here are a few examples.

Piping a variable to an Action that takes one argument.

    "Hello From AWright18.PipeTo".PipeTo(Console.WriteLine);

Piping a variable to an Action that takes two arguments.

    "Hello From {0}".PipeTo(Console.WriteLine,"AWright18.PipeTo");

Piping a variable to a Func that takes one argument and returns another argument

    var helloWorld = "Hello From AWright18.PipeTo".PipeTo((s) => s);

Piping a variable to a Func takes two arguments and returns another argument

    var helloWorld = "Hello From {0}".PipeTo(string.Format,"AWright18.PipeTo");

You can also create a pipeline of functions as before

static void Main(string[] args)
{
    "Hello"
        .PipeTo(AddFrom)
        .PipeTo(AddWho, "Awright18.PipeTo")
        .PipeTo(Console.WriteLine);
}

static string AddFrom(string s)
{
    return s + " from";
}

static string AddWho(string s, string who)
{
    return s + " " + who;
}

Lambda expressions are also supported.

	static void Main(string[] args)
	{
		"Hello"
			.PipeTo((value) => value + " from")
			.PipeTo((value,who) => value + " " + who,"AWright18.PipeTo")              
			.PipeTo(Console.WriteLine);
	}

Conclusion

This was a feature in F# I really love, and thought it might come in handy in C#. Try it out and let me know what you think.

Spread The Word
Adam.Wright
 

Adam Wright is a technologist specializing in software development using Microsoft technologies.

Click Here to Leave a Comment Below 0 comments