fuckyeahterminals: C# is a sink for language features
Sort of a throw back to This Post About Conditionals since the learning curve is “high” for Haskell, I thought I’d bring it to you in a language you might be more familiar with.
For those of you that know python or haskell this is what the commands look like
Python Haskell C#
map map Enumerable.Select
filter filter Enumerable.Where
reduce foldl Enumerable.AggregateSo in C# it would look a little something like this.
Func<int, bool>[] conditions = { (x) => x <= 100, (x) => x > -3, (x) => x != 5 }; int toTest = 10; IEnumerable<bool> results = conditions.Select(f => f(toTest)); bool meetsRequirements = results.Aggregate((x, y) => x && y); if (meetsRequirements) { //Do Stuffs System.Diagnostics.Debug.WriteLine("Met Requirements"); }Alright so what’s going on here…
Because function aren’t first class in C# there’s a little prep work that needs to be done.
First you’ll notice the Func<int, bool> signature, this is how it handles Functions, it says this is a Function object that takes in an integer and returns abool. You’ll notice I can call .Select on an array, that makes sense because an array is enumerable and therefore allows me to use the interfaceIEnumerable. Here’s where I feel things get murky, I have to catch the results of the select into an IEnumerable, I can’t catch it in an array of bool, that’s some OOP bullshit right there, fine, whatever. and then I can foldl it or “.Aggregate” it by and to see if it meets all my requirements.
I feel this is cleaner and more concise, especially in the case of validation for weird business logic, you can feed a list of (field, type) tuples that you can insert into various conditions that would give you a clean result. As opposed to writing if statement after if statement after if statement….



