Today a colleague and I were going through some code. I have recently been trying to impart to him the power and beauty in the functional programing paradigm in C#. Today, the opportunity to demonstrate it presented itself as I was showing him how to use extension methods to extend Entities and ValueObjects with a suite-to-purpose functional API. As we began the code, I realized that what I really wanted to show him was the concept with no additional fluff. First, the extension method part.
Here’s what I came up with:
public static class IntExtensions { public static void Times(this int count, Action<int> action) { for (var i = 0; i < count; i++) { action(i); } } }
This small extension to the builtin int type, gives us a convenient and expressive functional API, driven directly from Int32 typed variables themselves. Now, we can use it as follows.
[TestMethod] public void SampleRepository_Can_Create_New() { 10.Times(i => { var sample = TestObjects.BuildSample(); SampleRepository.Save(sample); }); var samples = SampleRepository.GetAll(); samples.Count().Times(i => Debug.WriteLine("your index is " + i)); Assert.IsTrue(samples.Count() == 10, "Should have 10 samples"); }
Rubyists recognize this API as its built into the language. Its simple stepwise iteration driven directly off numeric types. In C# we can use extension methods to shim this behavior into our scalers. APIs that read like 5.Times(doSomething); read like English. This makes our code more comprehensible by everyone, and that ladies and gentlemen is worth its weight in Gold Pressed Latinum
2 thoughts on “Giving Your Data Some Higher Order Muscle With C#”