In this article we will look at helper extension methods of StringBuilder first to better support chaining StringBuilder.
We will work on the same StringBuilder instance and add support for appending lines or character to the StringBuilder given a
condition. Also example showing how to aggregate lines from a sequence is shown and appending formatted lines. Since C# interpolation
has become more easy to use, I would suggest you keep using AppendLine instead.
Here is the helper methods in the extension class :
public static class StringBuilderExtensions {
public static StringBuilder AppendSequence<T>(this StringBuilder @this, IEnumerable<T> sequence, Func<StringBuilder, T, StringBuilder> fn)
{
var sb = sequence.Aggregate(@this, fn);
return sb;
}
public static StringBuilder AppendWhen(this StringBuilder @this, Func<bool> condition, Func<StringBuilder, StringBuilder> fn) =>
condition() ? fn(@this) : @this;
public static StringBuilder AppendFormattedLine(
this StringBuilder @this,
string format,
params object[] args) =>
@this.AppendFormat(format, args).AppendLine();
}
Now consider this example usage:
voidMain()
{
var countries = new Dictionary<int, string>{
{ 1, "Norway" },
{ 2, "France" },
{ 3, "Austria" },
{ 4, "Sweden" },
{ 5, "Finland" },
{ 6, "Netherlands" }
};
string options = BuildSelectBox(countries, "countriesSelect", true);
options.Dump("Countries"); //dump is a method available in Linqpad to output objects
}
privatestaticstringBuildSelectBox(IDictionary<int, string> options, string id, bool includeUnknown) =>
new StringBuilder()
.AppendFormattedLine($"<select id=\"{id}\" name=\"{id}\">")
.AppendWhen(() => includeUnknown, sb => sb.AppendLine("\t<option value=\"0\">Unknown</option>"))
.AppendSequence(options, (sb, item) => sb.AppendFormattedLine("\t<option value=\"{0}\">{1}</option>", item.Key, item.Value))
.AppendLine($"</select>").ToString();
What if we wanted to inspect the state of the stringbuilder in the middle of these chained expression. Is it possible to output state in such lengthy chained functional expressions?
Yes, that is called the Tee method inside functional programming patterns. Other might call it for Tap such as used in Rx languages.
The Tee method looks like this:
publicstaticclassFunctionalExtensions {
publicstatic T Tee<T>(this T @this, Action<T> act) {
act(@this);
return @this;
}
}
We can now inspect state in the middle of chained expressions in functional expressions.
So there you have it, if you have lengthy chained functional expressions, make such a Tee helper method to peek into the state this far. The name Tee stems from the Unix Command by the same name. It copies contents from STDIN to STDOUT.
More about Tee Unix command here:
I looked at encapsulating Using statements today for functional programming and how to look up the current time with API available on the Internet.
publicstaticclassDisposable {
publicstatic TResult Using<TDisposable,TResult>(
Func<TDisposable> factory,
Func<TDisposable, TResult> map)
where TDisposable : IDisposable
{
using (var disposable = factory()){
return map(disposable);
}
}
}
voidMain()
{
var currentTime = EpochTime.AddSeconds(Disposable
.Using(() => new HttpClient(),
client => JsonDocument.Parse(client.GetStringAsync(@"http://worldtimeapi.org/api/timezone/europe/oslo").Result))
.RootElement
.GetProperty("unixtime")
.GetInt64()).ToLocalTime(); //list of time zones available here: http://worldtimeapi.org/api/timezone
currentTime.Dump("CurrentTime");
}
publicstatic DateTime EpochTime => new DateTime(1970, 1, 1);
The Disposable is abstracted away in the helper method called Using accepting a factory function to create a TDisposable that accepts an IDisposable.
We look up the current time using the WorldTimeApi and make use of extracting the UnixTime which is measured from Epoch as the number of seconds elapsed from 1st January 1970.
We make use of System.Text.Json here, which is part of .NET to parse the json retrieved.
This article will look into helper methods for currying functions in C#. The definition of Currying consists of splitting up a function with multiple arguments
into multiple functions accepting one argument. But you can also have some of the arguments provided via smaller functions, so be aware also of this alternative.
What is in the name currying? The name has nothing to do with cooking from India, but comes from the mathematician Haskell Brooks Curry (!)
We will see in the examples that we can provide multiple arguments at once and the syntax will look a bit special compared to other C# code.
Curryings benefits is to allow a more flexible way to call a method. You can store into variables calls to a function providing a subset of argument
and use that variable to either specify an intermediate other call or get the final result.
Note - The function will be called when ALL arguments are provided ONCE ! This helps a lot of avoiding surprising side effects.
Let's first look at a sample set of methods we want to support currying.
We want to call the sample methods above in a more flexible way by splitting the number of arguments we provide.
Let's see the extension methods to call up to four arguments to a function. Note the use of chaining the lambda operator (=>) to provide
the support for currying.
The following main method shows how to use these curry helper methods:
voidMain()
{
var curryOneArgsDelegate = new Func<string, int>((st) => FooOneArgs(st)).Curried();
var curryOneArgsPhaseOne = curryOneArgsDelegate("hello");
var curryTwoArgsDelegate = new Func<string, float, int>((st, x) => FooTwoArgs(st,x)).Curried();
var curryTwoArgsPhaseOne = curryTwoArgsDelegate("hello");
var curryTwoArgsPhaseTwo = curryTwoArgsPhaseOne(3.14f);
var curryThreeArgsDelegate = new Func<string, float, int, int>((st, x, j) => FooThreeArgs(st, x, j)).Curried();
var curryThreeArgsPhaseOne = curryThreeArgsDelegate("hello");
var curryThreeArgsPhaseTwo = curryThreeArgsPhaseOne(3.14f);
var curryThreeArgsPhaseThree = curryThreeArgsPhaseTwo(123);
//Or call currying in a single call passing in two or more parametresvar curryThreeArgsPhaseOneToThree = curryThreeArgsDelegate("hello")(3.14f)(123);
var curryFourArgsDelegate = new Func<string, float, int, int, int>((st, x, j, k) => FooFourArgs(st, x, j, k)).Curried();
var curryFourArgsPhaseOne = curryFourArgsDelegate("hello");
var curryFourArgsNextPhases = curryFourArgsPhaseOne(3.14f)(123)(456); //just pass in the last arguments if they are known at this stage
curryFourArgsDelegate("hello")(3.14f)(123)(456); //you can pass in 1-4 parameters to FooFourArgs method - all in a single call for example or one by one
}
The output we get is this. Note that we only call the methods we defined when all parameters are sent in. The function call which had partial argument list provided did not result into a function call.
So from a higher level, currying a function f(x,y,z) means adding support that you could call the function like this:
f(x,g(y,z)) or f(x,g(y,h(z))) - there more arguments you get there is more variations of number of parameters and methods you can pass in.
Here is another example how you can build up a calculation uing simpler methods.
voidMain()
{
Func Area = (x,y) => x*y;
Func CubicArea = (x,y,z) => Area.Curried()(Area(x,y))(z);
CubicArea(3,2,4); //supplying all arguments manully is okay
}
CubicArea expects THREE arguments. The implementation allows us to use the Area function and via currying we can use that method and provide the last third argument avoiding compilation error.
Currying makes your functions allow more flexible ways of being called.