tag:blogger.com,1999:blog-15136575.post1394506758877349..comments2023-10-17T12:00:16.772+01:00Comments on Code rant: Fun with Linq AggregateMike Hadlowhttp://www.blogger.com/profile/16441901713967254504noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-15136575.post-59541600197438837792011-01-26T10:25:07.599+00:002011-01-26T10:25:07.599+00:00I'm late to the party, but I'd like to poi...I'm late to the party, but I'd like to point out that any call to Aggregate() with a List as accumulator can be replaced by a simple Select() followed by ToList(), which is much more readable and doesn't require an Append() extension method to be defined.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-24343813183755652152009-11-24T11:42:41.687+00:002009-11-24T11:42:41.687+00:00Interesting set of comments. I'd do it a total...Interesting set of comments. I'd do it a totally different way if I wanted to do it with LINQ (ignoring the debate about code readability)(apologies for the formatting):<br /><br /> string csv = <br /> @"1,2,3,4,5<br /> 6,7,8,9,10<br /> 11,12,13,14,15<br /> 16,17,18,19,20";<br /><br /> var res = csv.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)<br /> .Select(line => line.Split(',')<br /> .Select(number => int.Parse(number)).ToList()).ToList();Richard ODhttps://www.blogger.com/profile/13855706515612188950noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-91514687718874409852009-11-19T15:33:53.451+00:002009-11-19T15:33:53.451+00:00Mike: not a problem. I enjoyed the challenge (and ...Mike: not a problem. I enjoyed the challenge (and learning more about C# and Python :)<br /><br />BTW, I'd like to say thanks for all the great WCF/Windsor stuff. It's helped me enormously as we've just recently switched to .NET. Now if only we can figure out how to get it all working on Rackspace Cloud.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-58694073574410794422009-11-18T21:25:03.158+00:002009-11-18T21:25:03.158+00:00Redbeard, I stand corrected! That's really coo...Redbeard, I stand corrected! That's really cool and shows how Python can do this in a far more concise style than C#.<br /><br />Thanks again for the example and for taking the time to put me right :pMike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-87104442004246712882009-11-18T17:01:05.372+00:002009-11-18T17:01:05.372+00:00Actually, it is a list of lists. csv.split("\...Actually, it <i>is</i> a list of lists. csv.split("\n") creates a list of strings. Call it los.<br /><br />Then, (line.split(",") for line in los) creates a generator that spits out a series of lists.<br /><br />Finally, the reduce appends each element of the list to the source list. In Python, append adds the actual object to the list. Add will split up the list and add individual elements. So the resulting value of data is:<br /><br />[['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], ['11', '12', '13', '14', '15'], ['16', '17',<br /> '18', '19', '20']]<br /><br />Incidentally, in the process of researching this I discovered that the reduce line can be changed to:<br /><br />data = [line.split(",") for line in csv.split("\n")]<br /><br />That removes the necessity for the list_append function. It also reminds me of the power of list comprehensions :) Makes the whole thing, not counting the csv definition, three lines.<br /><br />Also, it should be noted that split() could be used instead of split("\n"), but that splits on all white space, and I wanted to be sure :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-85750421729603445982009-11-17T20:59:39.919+00:002009-11-17T20:59:39.919+00:00Redbeard, cool thanks for that, it's interesti...Redbeard, cool thanks for that, it's interesting to see how it would be done in Python.<br /><br />I have to point out though, that the data structure you parse your csv into is different from my example. It's a single list rather than a list of lists in my case.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-58020607919675655532009-11-17T09:11:17.010+00:002009-11-17T09:11:17.010+00:00As a newcomer to both C# and Python, I had to take...As a newcomer to both C# and Python, I had to take up the challenge of doing this in Python. There might be a better way, but this worked for me (I apologize for the slightly broken formatting...):<br /><br />import operator<br /><br />csv = """1,2,3,4,5<br />6,7,8,9,10<br />11,12,13,14,15<br />16,17,18,19,20"""<br /><br />def list_append(list, elem):<br />. . . .list.append(elem)<br />. . . .return list<br /><br />data = reduce(list_append, (line.split(",") for line in csv.split("\n")), [])<br /><br />output = " ".join(reduce(operator.concat, data))<br /><br />print(output)<br /><br /><i>(The ". . . ." construct means "indent 4 spaces", which the comment won't let me do :))</i><br /><br />If list.append() returned the list or of lambdas could include two statments, the list_append helper wouldn't be needed. But, maybe there's a guru who knows more than I.<br /><br />Of course, this is still sub optimal. Loops would probably be better. But for the first reduce, that's just still:<br /><br />data = []<br />for line in csv.split("\n"):<br />. . . .data.append(line.split(","))<br /><br />Thanks for the challenge!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-26625296713026431802009-11-17T09:03:05.153+00:002009-11-17T09:03:05.153+00:00It's clever and I like it - but I probably won...It's clever and I like it - but I probably won't do it.<br />I worry about recovering from formatting errors in the input file - easy to handle in old fashioned code, less so in fancy linq syntax?Unknownhttps://www.blogger.com/profile/16718171574642484199noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-80992632732206347802009-11-16T20:56:02.527+00:002009-11-16T20:56:02.527+00:00I guess readability has something to do with famil...I guess readability has something to do with familiarity. If you'd only ever had Aggregate methods and suddenly foreach was introduced, you'd all be complaining about that :)<br /><br />I do agree though that the first statement is uglier than the comparable nested foreach loops, but I think that's more to do with the clunky C# syntax than with nature of the thing. I should have added an F# (or Haskell or Erlang) example to show much cleaner it looks when your language is functional from the ground up.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-57187781975099120492009-11-16T13:34:59.174+00:002009-11-16T13:34:59.174+00:00Pretty cool - even though it is just for fun :-) ...Pretty cool - even though it is just for fun :-) (A point which some of your other readers seem to miss..)Unknownhttps://www.blogger.com/profile/02662668351496826313noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-25172091173104220672009-11-16T08:35:33.192+00:002009-11-16T08:35:33.192+00:00The first code is much more readable !
Your 2 stat...The first code is much more readable !<br />Your 2 statements are awful, we just can not imagine what is going down.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-89012836352131713272009-11-15T22:51:07.887+00:002009-11-15T22:51:07.887+00:00DO you think that this way of writing the code lik...DO you think that this way of writing the code like this will be readable ... it is not readable at all. what do you thik?Mahmoud Ghozhttps://www.blogger.com/profile/00716531616607664103noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-82961095924818602232009-11-15T12:31:44.666+00:002009-11-15T12:31:44.666+00:00Aggregate is one of those helper functions that...Aggregate is one of those helper functions that's overused when you first learn it because it's so cool and allows you to be so clever. I'd rather maintain the first code example, even with the pedestrian foreach statements.Matthttps://www.blogger.com/profile/17246165478042891607noreply@blogger.com