LINQ analogues in Scala?
Are there any sane analogues to L开发者_StackOverflow社区INQ (.NET) exists for Scala?
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.)
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)
Slick
is a modern database query and access library for Scala. (http://slick.typesafe.com/)
@table("COFFEES") case class Coffee(
@column("COF_NAME") name: String,
@column("SUP_ID") supID: Int,
@column("PRICE") price: Double
)
val coffees = Queryable[Coffee]
// for inserts use lifted embedding or SQL
val l = for {
c <- coffees if c.supID == 101
// ^ comparing Int to Int!
} yield (c.name, c.price)
backend.result( l, session )
.foreach { case (n, p) => println(n + ": " + p) }
There are many situations in Scala where you can use monadic constructs as a sort of query language.
For example, to query XML (in this case, extracting URLs from links in some XHTML):
def findURLs(xml: NodeSeq): Seq[URL] =
for {
a <- xml \\ "a"
href <- a attribute "href"
url <- href.text
} yield URL(url)
For an analogue of LINQ to SQL, the closest thing is probably ScalaQuery. To lift an example right out of the docs:
val q4c = for {
u <- Users
o <- Orders if o.userID is u.id
} yield u.first ~ o.orderID
精彩评论