Lambda Collection Wrappers

April 26, 2018 c# 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 .net c# 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

Include

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

Poor Man's Template Engine in C# Part 1

Groundwork

If you're looking for a poor man's solution to a templating engine for .net and don't really need the overhead a complete template engine brings with it, I've come up with the following solution.

Read More