tag:blogger.com,1999:blog-8715120618224381002.post7924857041620641961..comments2024-01-31T05:36:46.775+00:00Comments on This is an EX blog...: Optimizing Aggregate for String ConcatenationDanielhttp://www.blogger.com/profile/11384619047799752406noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8715120618224381002.post-3809664056518591402009-01-16T03:28:00.000+00:002009-01-16T03:28:00.000+00:00Yes, the 'x' should be an 'a' and I left out the c...Yes, the 'x' should be an 'a' and I left out the closing ')' to the Append method but that notwithstanding I'm not sure I see your point, the title is "Optimizing Aggregate for String Concatenation" not "Turning Optimization on/off By Merely Adding/Removing a Using Statement". Hidden optimization code can be unpredictable and a maintenance nightmare. I've ran into the occasional compiler optimization that breaks code (that why you can shut it off ;)).<BR/><BR/>Don't misunderstand your code is fairly cool. But as you said "it looks pretty ugly", if there were some type of bug in your optimization (I'm sure there isn't but I occasionally have some bugs in mine) someone trying to debug it could easily overlook the 'using' statement and wonder why code doesn't run as expected.<BR/><BR/>davesemAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8715120618224381002.post-12916260080845196122009-01-15T22:36:00.000+00:002009-01-15T22:36:00.000+00:00The key point is the ability to switch an optimiza...The key point is the ability to switch an optimization on/off by merely adding/removing a using statement. It's that general idea I was trying convey, not just as a solution for string concatenation. Obviously there are a ton of ways to use a StringBuilder to solve this particular problem. <BR/><BR/>As for what's wrong with it (you did ask!) should that 'x' be an 'a'? Also it has a slight maintenance gotcha, as I might change the length of the delimiter string and forget to also change the 2 - so make that a named variable. And personally I think it's kind of ugly anyway, with the way it removes something it just appended.Danielhttps://www.blogger.com/profile/11384619047799752406noreply@blogger.comtag:blogger.com,1999:blog-8715120618224381002.post-71392863328253953032009-01-14T17:02:00.000+00:002009-01-14T17:02:00.000+00:00What's wrong with:Enumerable.Range(0, size ).A...What's wrong with:<BR/><BR/>Enumerable.Range(0, size ).Aggregate(new StringBuilder(),<BR/>(a, b) => a.Append(", " + b.ToString(),<BR/>(a) => x.Remove(0,2).ToString());<BR/><BR/>This works with any type and allows you to call any function for that type that returns its value as a string.<BR/><BR/>davesemAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8715120618224381002.post-61060436252030632472008-12-10T16:34:00.000+00:002008-12-10T16:34:00.000+00:00I am seriously considering that! Or at least allow...I am seriously considering that! Or at least allowing the parameter expressions to be any expression in terms of that parameter, so Substring/Trim etc would be useable.Danielhttps://www.blogger.com/profile/11384619047799752406noreply@blogger.comtag:blogger.com,1999:blog-8715120618224381002.post-62360278579102349082008-12-10T16:22:00.000+00:002008-12-10T16:22:00.000+00:00Oh come on :P the correct implementation should ch...Oh come on :P the correct implementation should check if the entire expression is an arbitrary tree of only string concatenation (and possibly Replace()/ Substring()) and generate a functionally equivalent expression using StringBuilders ;)Jimmyhttps://www.blogger.com/profile/12199609007558588370noreply@blogger.com