{
Friend and coworker Algotron just posted an intro and sample of currying with C#. I decided to try out currying with the idea of successive regular expressions on a string array - I found it easy to approach first with anonymous delegate syntax and then use lambda expressions. Once this was in place it was easy to understand the use of an extension method to curry any binary function.
Func<string, string[], Func<string, string[]>> fil =
delegate(string pattern, string[] lines){
return delegate(string pattern2)
{
return lines
.Where(p=> Regex.IsMatch(p, pattern))
.Where(p => Regex.IsMatch(p, pattern2))
.ToArray();
};
};
Func<string, string[], Func<string, string[]>> regFil =
(pattern, input) =>
(pattern2) =>
input
.Where(p => Regex.IsMatch(p, pattern))
.Where(p => Regex.IsMatch(p, pattern2))
.ToArray();
// assume some address data
string[] data = new string[]{"1234 Somewhere", "this", "777 dakota"};
var filterNumeric = regFil(@"\d", data); // make sure it has a number
var filterUCase = filterNumeric("[A-Z]"); // make sure it has an uppercase character
Console.Write(String.Join(",", filterUCase));
That's cool stuff, worth looking at for a while to try to understand.
No comments:
Post a Comment