4 ms·
I have little knowledge of the latest trends in programming languages, but if your example of a good PL theorist is someone who designed LINQ, then I am going t
by BobTurbo 15y ago
I have little knowledge of the latest trends in programming languages, but if your example of a good PL theorist is someone who designed LINQ, then I am going to have to take your opinion with a grain.OfType(Salt).Where(m => m.Value.Contains(nothing)).ToList<Salt>().OrderBy(m => m.irrelevance).FirstOrDefault<Salt>();
- olavk 15y agoDo you have a specific criticism of Linq, or suggestions to how it could have been designed better?
- BobTurbo 15y agoYes, it requires a wide-screen monitor. I don't think I can answer the other question as I don't see any value in it. Turning loops and so on into a giant, inflexible (unless you want two wide-screen monitors) daisy-chain of methods does not improve the readability or maintainability of code. It also encourages programmers to perform actions that should otherwise be in "repository" classes, in inappropriate places. The from item in blah syntax is just SQL rearranged, and SQL isn't as expressive as programming language constructs, so why try to emulate it? It encourages you to write the grain of salt statement I mentioned earlier, which requires programmers to read through and understand the implementation of what you are doing, instead of it being embodied in a meaningful method and called. Of course you could just wrap that in a method, and slowly grow the line to 2-3 wide-screen monitors as you add more conditions. The more tools you add to the core of a language, the more of a monstrosity of a kitchen sink it becomes (c#/.NET). Now you can loop over a list in 6 different ways! Hooray! That is why Dart looks good - a few basic concepts that can be used to build things suited to a particular problem.
- olavk 15y agoWell, if your example were meant seriously, it is needlessly verbose. ToList is not necessary in this context, and the type arguments to ToList and FirstOrDefault are not necessary since they can be inferred. And anyway, you can just insert line breaks if you think the chain is too long. So: grain.OfType<Salt>() .Where(m => m.Value.Contains(nothing)) .OrderBy(m => m.irrelevance) .FirstOrDefault(); I don't really get your verbosity criticism, sine any alternative I can think of (eg. using for loops) is much more verbose.
- cubicle67 15y agoI'm not familiar with LINQ, but is that roughly equivalent to grains.select{|g| g.is_a?(Salt) && g.value.blank? }.sort{|a,b| a.irrelevance <=> b.irrelevance }.first
- BobTurbo 15y agovar saltGrains = new List<Salt>(); foreach (var g in grains) { if (g.TypeOf() == "Salt" && g.Value == nothing) saltGrains.Add(g); } saltGrains.sort(m => m.Value.irellevance); return saltGrains.First(); or something.
- jeltz 15y agoRuby tip: Instead of .sort you can use .sort_by. grains.select{|g| g.is_a?(Salt) && g.value.nil? }.sort_by(&:irrelevance).first Edit: Changed from blank? to nil? since that is probably closer to nothing in C#.
- cubicle67 15y agothanks. I've been writing Ruby for a few years now and I never knew about sort_by. Looks like is works in 1.8 too, but without the &: syntax sort_by{|g|g.irrelevance} source: http://www.ruby-doc.org/core-1.9.2/Enumerable.html#method-i-sort http://www.ruby-doc.org/core-1.9.2/Enumerable.html#method-i-...
- jeltz 15y agoI actually believe the &:symbol syntax was added in 1.8.7 since I am 100% positive we use that in our codebase which still has not been ported to 1.9. $ ruby -e 'puts VERSION; p ["10", "2", "3"].sort_by(&:to_i)' 1.8.7 ["2", "3", "10"]
- koenigdavidmj 15y agoRails adds it too, even if your version of Ruby does not support it. Yay monkeypatching! EDIT: said duck typing first. It's too early for thinking.
- politician 15y agoActually, you would write that query as: var q = from m in grains.OfType<Salt>() where m.Value.Contains(null) orderby m.irrelevance select m; return q.FirstOrDefault(); which fits in 80 characters on one screen. From this post and your reply, it seems like the your complaint is that LINQ is hard to understand if you write it poorly. But that's true of most programming.
- BobTurbo 15y agoYes if you write it like that, it removes one of my complaints, but it leaves the other. You now have inconsistency in the language because LINQ is basically being used as a new type of language added to an existing language. You are working with two languages at once. Why stop there? Why not add python to c#? And every other language. The super language that has everything from everywhere. It is a total mess.
- politician 15y agoLINQ solves a real business need -- how to access sets of data, regardless of the source, in a uniform teachable way. If you can teach people how to query data structures using a one syntax that is integrated into the language, then you don't have to teach them a different API for every different data source. By encouraging the few API designers to target a standard, the many API consumers can build more reliable code faster. If you approach it as what it is, yes, a language-integrated query syntax, and avoid the fluent-chaining style most of the time, then you'll see an information-dense productivity booster instead of a "total mess". The LINQ syntax fits on 1 sheet of paper: http://www.albahari.com/nutshell/linqsyntax.aspx Personally, I look at the two-tier language and library support as a case of "the easy things should be simple, and the hard things possible." That's just my own take, but I find that it guides thought process a little better. I look for a syntax solution first, then rewrite it using a chaining style if I need to (e.g. SelectMany), or possibly split into multiple queries. I don't think I've ever written one longer than 10 lines, properly formatted. Now, as far as python and other languages are concerned, LINQ is less "python in C#" and more "PEP 202 in C#".