I want to compose a sequence of functions that when given a string passes it through all the created functions and produces a modified string. e.g.
string[] arr = {"po", "ro", "mo", "do"};
var modify = "pomodoroX";
foreach (var token in arr)
{
modify = modify.Replace(token, "");
}
Console.WriteLine(modify); // Output: X
This solves the problem, but I am interested in the Functional solution:
Console.WriteLine(
arr.Select<string, Func<string, string>>(val => (s1 => s1.Replace(val, string.Empty)))
.Aggregate((fn1, fn2) => fn1 += fn2)
.Invoke("pomodoroX")
);
// Output: pomoroX -> Only last element applied because:
// the functions are not getting combined.
So basically, take the array "arr" and for each string create a function that removes that string.
The current solution is flawed and applies only the last function and I can't seem to convert this to delegates so as to combine them with += operator.
Or are there better functional solutions?