Is this madness? The pursuit of single-statement methods
-
Yeah, it's tricky isn't it. Documentation doesn't seem to state that order is guaranteed not to change, but implementation indicates that's the case. I can't really imagine how you can improve much on that implementation either (which is strikingly similar to the initial posted implementation), so it's probably fair to assume no reordering will occur. But without that cast-iron guarantee, a future version of .NET could scupper things. Well hey, that's what consultancy rates are for. The bit which has me intrigued now is the Set class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.
Regards, Rob Philpott.
Rob Philpott wrote:
The bit which has me intrigued now is the Set<t> class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.
Yes, it's an internal class within the
Enumerable
class: Enumerable.Set<TElement>[^] I'd have used aHashSet<T>
as well, but I guess MS were probably writing theDistinct
method before theHashSet
class was finished. Either that, or there were some very specific performance issues they were trying to work around. But if that was the case, I'd have expected to see a comment explaining the problem they were trying to solve.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
My code started out as something fairly easy to follow like this:
void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
{
// Create a set to hold a collection of lines that have already been processed.
SortedSet seen = new SortedSet();
// Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
StringBuilder stringBuilder = new StringBuilder();
// Split the text from the text box by lines and examine each one.
foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
{
// If the string is successfully added to the set, then it has not been processed before.
if (seen.Add(line))
{
// Add the line to the output.
stringBuilder.AppendLine(line);
}
}
// Build the string and assign it to the text box.
this.InputTextBox.Text = stringBuilder.ToString();
}Then I factored out some of the code into extension methods, removed the unnecessary curly braces, and got this:
void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
{
// Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
StringBuilder stringBuilder = new StringBuilder();
// Split the text from the text box into a collection of unique lines.
foreach (string line in this.InputTextBox.Text.SplitLines().Unique())
// Add the line to the output.
stringBuilder.AppendLine(line);
// Build the string and assign it to the text box.
this.InputTextBox.Text = stringBuilder.ToString();
}And then I took it a step further.
void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) =>
// Take the text from the text box, split it by lines, remove the duplicates, and assign the results to the text box.
this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(
new StringBuilder(),
(StringBuilder stringBuilder, string line) => stringBuilder.AppendLine(line),
(StringBuilder stringBuilder) => stringBuilder.ToString());And the one-liner:
void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());
I get great joy a
Interesting!! I have never seen a method, like your RemoveDuplicatesButton_Click() use the => syntax at this point?! That actually works?
Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach