A neat way to curry in C#

May 25, 2018c#fp

I was reading an older blogpost from Mike Hadlow about Partial Application in C# in which he discusses how Partial Application can be implemented in C# via Currying. Although I appreciate his example of implementing currying via extsion methods, the syntax is hideous. There is a suggestion in the comments though that I found to be a much better solution.

// Define a local function Add.
int Add(int a, int b) => a + b;

// Here we do the currying.
Func<int, int> add3 = (b) => Add(3, b);

// This will print 5.

// Curry one more time so that we have
// a function that simply produces 5.
Func<int> five = () => add3(2);

// This will also print 5.

Lambda Collection Wrappers

April 26, 2018c#fp

I've been reading a lot as of late about functional programming and I try to incorperate as much as possible into my everyday programming. One trick that I've definitely started using is wrapping collections in lambda functions.

Read More

string.Join Extension Method

April 19, 2018.netc#linq

Normally if want to do a string.Join on the result of a Linq query you end up with somthing looking like this:

Console.WriteLine(string.Join(", ", numbers.Where(x => x % 2 == 0)));

The call to string.Join has to come first and then the Linq query. I've always felt this breaks the flow of the code and would be easier to read if the string.Join was at the end of the Linq query:

Console.WriteLine(numbers.Where(x => x % 2 == 0).JoinString(", "));

This can be implemented with the following extension methods:

public static class JoinStringExtensions
    public static string JoinString<T>(this IEnumerable<T> source, string seperator) =>
        string.Join(seperator, source.Select(x => x.ToString()));

    public static string JoinString(this IEnumerable<string> source, string seperator) =>
        string.Join(seperator, source);

The specialization for IEnumerable<string> is just mirroring the implementation from string.Join.

Poor Man's Template Engine in C# Part 3


In our last episode we implemeted the first two helper funcitons HtmlEncode and If. Today we want to implement Include.

Read More

Poor Man's Template Engine in C# Part 2

HtmlEncode and If

In our last episode we laid the groundwork for our simple template engine. In this episode we'll introduce our first 2 helper functions.

Read More