4 ms·
C#'s LINQ (query syntax, not methods) got it right var result = from s in stringList where s.Contains("Tutorials") select s;
by tester756 3y ago
C#'s LINQ (query syntax, not methods) got it right
var result = from s in stringList
where s.Contains("Tutorials")
select s;
- DaiPlusPlus 3y agoor just `var result = stringList.Where( s => s.Contains("Tutorials") )` I can't stand the non-extension-method Linq syntax: the _only_ place where it offers a readability improvement over ext-methods is using `join` - but I hardly ever do that in Linq anyway. Also, in both my code and yours, `result` will be a lazy-evaluated `IEnumerable<String>` which may be undesirable - which means it's probably a good idea to use `.ToList()` to materialize it - which means having to use ext-methods anyway - and mixing both syntaxes in the same expression is aesthetically atrocious.
- rjbwork 3y ago>I can't stand the non-extension-method Linq syntax: the _only_ place where it offers a readability improvement over ext-methods is using `join` Hah! I tell the juniors this all the time :D. This seems to be basically the consensus among the C# community these days as far as I can tell as well.
- recursive 3y agoI guess I'm not in the C# community. `let` is pretty annoying to reproduce with extension methods. Also, when you use multiple `from` clauses, you get access to all scopes, whereas with `SelectMany` you only get the parameters of your current lambda.
- DaiPlusPlus 3y ago> you get access to all scopes Read: "you create new heap-allocated closures which wreck your Linq expression's runtime performance" Or: "you create Linq queries that cannot be translated into SQL"
- recursive 3y agoIt was all in memory. To the extent that it's a performance tradeoff, it might be one that's worth making depending on the use.
- DaiPlusPlus 3y agoAye - but the annoying thing is that it was never necessary: with a few subtle changes to Linq it’s possible to have allocation-free closures by passing state via hidden parameters on the stack - but just like every language out there we’re now hobbled by decisions made 15 years in the past. —— On a related note, it’s interesting just how unpopular so many new C# language features are (just by looking at the numbers of Thumbs-down reactions on the GitHub Issues/PRs - stuff like top-level Main. It feels like C#’s LDT wants to be like Swift, but without Swift’s willingness to ditch ill-conceived features after a few years… but I think C# would be well-served by taking an axe to some language-features by now - like keyword-Linq and CLS-compliance (honestly, do any ISAs today still lack hardware support for unsigned ints?)
- recursive 3y agoInteresting. I think there are some otherwise-seldom-used linq overloads for doing just that. Maybe on Join() or GroupJoin() or something like that. Query expressions use them for compilation to keep closure use down, but I don't know too much about it. Anyway, if top-level statements are wrong, I don't ever want to be right. I had no idea there was any controversy on that. It's trivial to add the boiler-plate back in if you want it. The main thing that bugs me is that Expression<> is stuck with language features that existed in C#4, even when there are trivial lowerings. e.g. `is not null` could be `!= null`. (This example ignores operator implementations, but most IQueryables ignore more than that already) Even better, add AST node types for the new language features.
- Jochim 3y agoI'm not sure the Github issues are the best reflection of the popularity of those features. Top level statements and the new HostBuilders are fantastic, they're much cleaner and easier to follow than the previous mess. Add Minimal APIs to the mix and C# is finally a viable choice for spinning up something quickly.
- Volrath89 3y agoI completely agree with you, in the context of C# code. But also take into account not everybody in HN is a C# developer, and OP syntax works better as an example of his point. All non C# devs will understand his "sql like" example easier.
- recursive 3y ago> the _only_ place where it offers a readability improvement over ext-methods is using `join` No love for let? from item in items let frob = Expenseive(item.P1) where frob > 3 selec new { frob, item }
- DaiPlusPlus 3y agoThat can be done with ValueTuples instead of Anonymous Types: `items.Select( i => ( i, frob: Expensive( i.P1 ) ) ).Where( t => t.frob > 3 );` ...and ValueTuples are superior to Anonymous Types because you can actually return them from a function or use them as parameters - whereas Anonymous Types cannot cross method-call boundaries (excepting using generics for pass-through). Anonymous Types in C# were a massive mistake. They should be [Obsolete]'d, IMO.
- recursive 3y agoThe tuple might be gone by the time the scope ends. I'm aware it's possible to reproduce the behavior without let. The code just looks worse.
- tester756 3y agochill, I've been just showing that *approach* of starting SQL query from "from" part is viable, because that's how Microsoft timplemented in one of two LINQ "API"s" I'm not trying to convince anyone to use LINQ's Query Syntax.
- Reitet00 3y agoI wonder if that could be addressed at the spec level allowing reverse order of these keywords. It doesn't seem complex on the surface and the the engines could slowly add support for it.
- christophilus 3y agoLINQ and proper reflection are what I miss most from C#. (I’m all Typescript at the moment.)
- rjbwork 3y agoGods, I tried to do some reflection stuff in a Node TS project I got put on about 18-24 months ago, and it was a total shit show. It was really disappointing to see how lacking the actual runtime capabilities of TS are.
- hfkwer 3y agoThere are no TS runtime capabilities. This isn't a thing.
- rjbwork 3y agoThere are some libraries that allow you to do some stuff. You can attach metadata to some objects about types in some contexts. But obviously it's bolted on because under the hood it's all Javascript.
- baq 3y agoWhat runtime capabilities? There shouldn’t be any?
- DaiPlusPlus 3y agoWhat does "proper reflection" mean to you?
- turboponyy 3y agoThe work was already done for them, as LINQ is just do-notation from Haskell.
- DaiPlusPlus 3y agoNo, Linq is *not* in any way like Haskell's `do`. Linq is a (reasonably) compromised, non-referentially-transparent, implementation of a restricted form of relational-algebra with side-effects permitted, so I'm not comfortable describing Linq as "monadic". Whereas Haskell's `do` is strictly monadic.
- ledgerdev 3y agoI recall Anders saying on some podcast they did that so they could provide auto-complete. Funny thing is that I used it for a couple years, but almost never use the linq syntax any more, and not sure why.
- cerved 3y agoProbably because if you wanted to write something like SQL, you would write it in SQL, and if you want to write C#, you write C# Linq query syntax is just dumb
- jmartrican 3y agoI'm so jealous that Java does not have its own LINQ.
- Luctct 3y agoTcl dicts do something very similar, and Tcl is very well integrated with SQLite.