LINQ analogues in Scala?
Are there any sane analogues to LINQ (.NET) exists for Scala?
Solution 1:
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...>
and Action<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
- a specification for a set of standard query operators
- a set of implementations for those operators (i.e.
IQueryable
, LINQ-to-XML, LINQ-to-SQL, LINQ-to-Objects) - a built-in embedded syntax for LINQ query comprehensions
- a monad
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: Select
is map
, Aggregate
is reduce
(or fold
), SelectMany
is flatMap
, Where
is filter
or withFilter
, orderBy
is sort
or sortBy
or sortWith
, and there are zip
, take
and takeWhile
and 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 for
loops. They are called for
comprehensions and are the equivalent to C#'s query comprehensions and Haskell's monad comprehensions. (They also replace C#'s foreach
and 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.)
Solution 2:
All LINQ IEnumerable
extensions are available in Scala. For example:
Linq:
var total = orders
.Where(o => o.Customer == "myCustomer")
.SelectMany(o => o.OrderItems)
.Aggregate(0, (sum, current) => sum + current.Price * current.Count);
scala:
val total = orders
.filter(o => o.customer == "myCustomer")
.flatMap(o => o.orderItems)
.foldLeft(0)((s, c) => s + c.price * c.count)