Are there any sane analogues to LINQ (.NET) exists for Scala?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on what exactly you mean by “LINQ”. LINQ is many things.
The most obvious answer would be: just use the .NET port of Scala. It gives you full native access to everything in .NET, which obviously includes LINQ.
Unfortunately, the .NET port of Scala was dropped a couple of years ago. Fortunately, it was picked up again a couple of months ago, with official funding directly from Microsoft no less. You can expect a release sometime in the 2011/2012 timeframe.
Anyway, what is LINQ?
A couple of features where added to .NET and specifically C# and VB.NET for LINQ. They are not technically part of LINQ, but are necessary prerequisites: type inference, anonymous (structural) types, lambda expressions, function types (
Func<T...>andAction<T...>) and expression trees. All of these have been in Scala for a long time, most have been there forever.Also not directly part of LINQ, but in C#, LINQ query expressions can be used to generate XML, to emulate VB.NET’s XML literals. Scala has XML literals, like VB.NET.
More specifically, LINQ is
IQueryable, LINQ-to-XML, LINQ-to-SQL, LINQ-to-Objects)In Scala, like in pretty much any other functional language (and in fact also pretty much any other object-oriented language, too), the query operators are simply part of the standard collections API. In .NET, they have a little bit weird names, whereas in Scala, they have the same standard names they have in other languages:
Selectismap,Aggregateisreduce(orfold),SelectManyisflatMap,WhereisfilterorwithFilter,orderByissortorsortByorsortWith, and there arezip,takeandtakeWhileand so on. So, that takes care of both the specification and the LINQ-to-Objects implementation. Scala’s XML libraries also implement the collections APIs, which takes care of LINQ-to-XML.SQL APIs are not built into Scala, but there are third-party APIs which implement the collection API.
Scala also has specialized syntax for those APIs, but unlike Haskell, which tries to make them look like imperative C blocks and C#, which tries to make them look like SQL queries, Scala tries to make them look like
forloops. They are calledforcomprehensions and are the equivalent to C#’s query comprehensions and Haskell’s monad comprehensions. (They also replace C#’sforeachand generators (yield return)).But if you really want to know whether or not there are analogues for LINQ in Scala, you will first have to specificy what exactly you mean by “LINQ”. (And of course, if you want to know whether they are “sane”, you will have to define that, too.)