Analogs LINQ in Scala?

Are there any reasonable LINQ (.NET) analogues for Scala?

+44
scala linq
Sep 24 '10 at 8:33
source share
4 answers

It depends on what exactly you mean by "LINQ". LINQ is a lot of things.

The most obvious answer: just use the .NET Scala port. This gives you full native access to everything in .NET, which obviously includes LINQ.

Unfortunately, the .NET Scala port was reset a couple of years ago. Fortunately, it was raised again a couple of months ago, with official funding directly from Microsoft no less. You can count on the release sometime in the 2011/2012 timeframe.

In any case, what is LINQ?

Several features added to .NET, namely C # and VB.NET for LINQ. They are not technically part of LINQ, but they are necessary prerequisites: output type, anonymous (structural) types, lambda expressions, function types ( Func<T...> and Action<T...> ) and expression trees. They were all in Scala for a long time, most of them were there forever.

It is also not a direct part of LINQ, but in C # LINQ query expressions can be used to generate XML, to emulate VB.NET XML literals. Scala has XML literals such as VB.NET.

More specifically LINQ

  • specification for a set of standard query operators
  • a set of implementations for these statements (i.e., IQueryable , LINQ-to-XML, LINQ-to-SQL, LINQ-to-Objects)
  • built-in built-in syntax for understanding LINQ queries
  • Monad

In Scala, like in any other functional language (and in fact also in almost any other object-oriented language), query operators are simply part of the standard APIs. In .NET they have slightly strange names, while in Scala they have the same standard names as in other languages: Select is map , Aggregate is reduce (or fold ), SelectMany - flatMap , Where - filter or withFilter , orderBy is sort or sortBy or sortWith , and there is zip , take and takeWhile , etc. Thus, this applies to both the specification and the implementation of LINQ-to-Objects. Scala XML libraries also implement collection APIs that take care of LINQ-to-XML.

SQL APIs are not built into Scala, but there are third-party APIs that implement collection APIs.

Scala also has specialized syntax for these APIs, but unlike Haskell, which tries to make them look like imperative C-blocks and C #, which try to make them look like SQL queries, Scala tries to make them look for . They are called for understandings and are equivalent to understanding C # queries and understanding Haskell monads. (They also replace C # foreach and generators ( yield return )).

But if you really want to find out if there are analogues for LINQ in Scala, you first need to specify what exactly you mean by "LINQ". (And, of course, if you want to know if they are "normal", you will also have to determine this.)

+53
Sep 24 2018-10-12T00:
source share

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) 
+37
Sep 24 '10 at 12:30
source share

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) } 
+9
Jan 11 '13 at 15:19
source share

There are many situations in Scala where you can use monadic constructs as a kind of query language.

For example, to request XML (in this case, extract 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 the LINQ to SQL counterpart, the closest thing is probably ScalaQuery . To remove an example directly from the documents:

 val q4c = for { u <- Users o <- Orders if o.userID is u.id } yield u.first ~ o.orderID 
+8
Sep 24 2018-10-10T00:
source share



All Articles