4 ms·
LINQ is largely based on FP stuff, also how Smalltalk collections work. It is relatively easy to find similar capabilities in most languages nowadays, unless o
by pjmlp 2y ago
LINQ is largely based on FP stuff, also how Smalltalk collections work.
It is relatively easy to find similar capabilities in most languages nowadays, unless one is stuck on Go, C and similar.
- sanex 2y agoDo you know an equivalent for Linw to EF in kotlin or Java because I have not found it.
- stanac 2y agoThose are linq expressions. They are indeed wonderful. You get an abstract tree from which you can create SQL or API commands to access the data source. I remember in the early days (.NET 3.5?) there were multiple examples of LINQ2X like Linq2Csv, Linq2Rss Linq2Drobox (I'm paraphrasing, I don't remember actual examples, but it was wild). There is also relinq library which transforms linq expressions into expressions which are easier to understand/use.
- pjmlp 2y agoRather quick search, as I am more of a myBatis person, Java: https://www.jooq.org/ https://www.jooq.org/ Kotlin: https://www.ktorm.org https://www.ktorm.org
- kumarvvr 2y agoAny pointers to such libraries in python?
- pjmlp 2y agoitertools would be the starting point, unfortunelly Python is rather limited due to the way it only supports one line lambdas.
- apwell23 2y agoNo it isn't easy to find similar capabitites in java, go, python, ruby. Maybe you do simulate some of this using meta programming in ruby but its certainly not 'easy to find'.
- pjmlp 2y agoIt certainly is, unless you are talking about SQL like syntax, which is basically syntax sugar for classical FP. And I explicitly left Go out of my list.
- apwell23 2y ago>unless you are talking about SQL like syntax yes thats what linq is? https://learn.microsoft.com/en-us/dotnet/csharp/linq/ https://learn.microsoft.com/en-us/dotnet/csharp/linq/ "Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language." With LINQ, a query is a first-class language construct, just like classes, methods, and events. doing this in java is not LINQ imo List<Integer> lowNums = filter(toList(numbers), new Predicate<Integer>() { @Override public boolean apply(Integer n) { return n < 5; } });
- high_na_euv 2y agoTo be fair, 99% of linq usage is method chaining syntax, not query syntax
- blackoil 2y agoOne difference with LINQ is its ubiquity. It works with database, in memory data structures, on disk files. You can use your skills/code across all the system.
- John23832 2y agoIt's just built on top of anything that is Iterable. If a language has first class iterator support, they could do something similar.
- naasking 2y agoMainly first-class functions I think. If you have those, you can just use fold in the core combinators.
- mythz 2y agoTakes a lot more than that, LINQ providers work by accepting a LINQ Expression Syntax tree instead of an opaque function, which allows providers to inspect and traverse the Expression's AST and translate it into the data source it's implementing. This Expression AST is constructed by the compiler, not something that can be tacked on by a library later.
- megadal 2y agoYes, but I think the point is practically every high level language can already do this pretty trivially. If it's scripted you can typically just get a string representation of the function. If it's Java, JAR inspection/dynamics have been a thing for a long time. And in other languages, they usually directly support metaprogramming (like Rust) and plugging code into the compilation logic.
- mythz 2y agoIf it were trivial you'd see LINQ-like providers implemented in "practically every high level language". Source code of the function means you have to implement the parser/lexer to convert it into a usable AST which is bad for both runtime performance and library size. Very much doubt this is available in Java, which Java ORM lets you use native Java language expression syntax to query a database?
- jiehong 2y agoI've found [0] for clojure, which maps the best IMO, but it also contains links to the same LINQ examples in other languages (java, kotlin, swift, elixir, python, ...). [0]: https://github.com/mythz/clojure-linq-examples https://github.com/mythz/clojure-linq-examples
- nightski 2y agoThe difference is many of those are dynamically typed languages. It's still useful, but a lot of the a beauty of LINQ comes from the fact that it is within a statically typed language.
- John23832 2y agoIt's surprising that Go didn't ship with it, but given that they just added iterators, it's coming. Rust has combinators, which is the same thing. Most new languages are recognizing that functional support (even if they don't consider themselves FP languages) is necessary.
- rw_panic0_0 2y agonah it's not coming, functions like maps and filters won't come to go by design, iterators are not only about FP stuff
- pjmlp 2y agoGo culture is quite clearly against this kind of niceties.
- devjab 2y agoYou can do functionality similar to LINQ with chaining as long as you don’t need to call a method with a generic different from the one defined. If you do need that, you’re going to have to do it without chaining. You can still do something similar but it’ll be a lot less elegant than how C# does it. It’s part of the design philosophy of Go though. They don’t want any magic. It’s similar to why they enforce explicit error handling instead of allowing you to chose between explicit and implicit. They want you to write everything near where it happens and not rely on things you can’t see. It’s probably the primary reason that Go is either hated or loved. I think it’s philosophy is great, a lot of people don’t. I have written a lot of C# over the years, so I’m a little atypical in that regard, I think most C# developers think Go is fairly inferior and in many regards they are correct. Just not in the ones that matter (come at me!). To elaborate a little on that, Go protects developers from themselves. C# is awesome when it’s written by people who know how it works, when it’s not you’ll get LINQ that runs in memory when it really shouldn’t and so on.
- whizzter 2y agoYes and no, the LINQ syntax being coherent between IEnumerable<> and IQueryable<> hides a lot of good magic. IEnumerable<> is regular in-memory lambdas/streams, same what you find in many places. IQueryable<> relies on the LINQ expressions, those CAN be JIT compiled for direct execution, but the fact that they are data-objects is what allows the translation to SQL and execution on the server rather than locally and can give massive gains since processing can be done where the data lives.
- whizzter 2y agoFor reference, to achieve what IQueryable does with 100% normal code in JavaScript you need something like Qustar that was posted here a month ago. Regular transform code in JS (Like IEnumerable) const ids = users.filter(user => user.age<18).map(user => user.id); IQueryable like to be transformed to the server: const ids = users.filter(user => user.age.lt(18)).map(user => user.id); In C# it'd look identical, but in JS or Java this would be achieved via proxy-object hacks (the .lt() function in the filter instead of the < operator and the .id property getter for the user map to send a flag under the hood for the mapping function). https://github.com/tilyupo/qustar https://github.com/tilyupo/qustar