Scala中的LINQ类似物? [英] LINQ analogues in Scala?

查看:88
本文介绍了Scala中的LINQ类似物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala是否存在与LINQ(.NET)相同的健全的类似物?

Are there any sane analogues to LINQ (.NET) exists for Scala?

推荐答案

这取决于"LINQ"的确切含义. LINQ有很多东西.

It depends on what exactly you mean by "LINQ". LINQ is many things.

最明显的答案是:只需使用Scala的.NET端口即可.它使您可以完全访问.NET中的所有内容,其中显然包括LINQ.

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.

不幸的是,几年前,Scala的.NET端口已被删除.幸运的是,几个月前又再次获得了它,而直接来自微软的官方资助也不少.您可以在2011/2012年的某个时间发布版本.

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.

无论如何,什么是LINQ?

Anyway, what is LINQ?

.NET中添加了几个功能,特别是LINQ的C#和VB.NET .从技术上讲,它们不是LINQ的一部分,但是必要的先决条件:类型推断,匿名(结构)类型,lambda表达式,函数类型(Func<T...>Action<T...>)和表达式树.所有这些都已经在Scala住了很长时间了,其中大多数都已经永远存在了.

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.

它也不是LINQ的直接组成部分,但是在C#中,LINQ查询表达式可用于生成XML,以模仿VB.NET的XML文字. Scala具有XML文字,例如VB.NET.

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.

更具体地说,LINQ是

More specifically, LINQ is

  • 一组标准查询运算符的规范
  • 这些运算符的一组实现(即IQueryable,LINQ-to-XML,LINQ-to-SQL,LINQ-to-Objects)
  • 用于LINQ查询理解的内置嵌入式语法
  • 一个单子
  • 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

在Scala中,就像几乎任何其他功能语言(实际上也几乎是任何其他面向对象的语言一样)一样,查询运算符只是标准集合API的一部分.在.NET中,它们的名称有点怪异,而在Scala中,它们具有与其他语言相同的标准名称:SelectmapAggregatereduce(或fold), SelectManyflatMapWherefilterwithFilterorderBysortsortBysortWith,并且有ziptaketakeWhile等等.因此,这既照顾了规范又照顾了LINQ-to-Objects的实现. Scala的XML库还实现了集合API,这些API负责LINQ-to-XML.

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.

Scala并未内置SQL API,但是有一些第三方API可以实现集合API.

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

Scala还为这些API提供了专门的语法,但是与Haskell试图使它们看起来像命令式C块和C#试图使它们看起来像SQL查询不同,Scala试图使它们看起来像for循环.它们称为 for理解,它们等效于C#的查询理解和Haskell的monad理解. (它们还替换了C#的foreach和生成器(yield return)).

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)).

但是,如果您真的想知道Scala中是否有LINQ的类似物,则首先必须具体说明"LINQ"的含义. (当然,如果您想知道它们是否理智",则也必须对其进行定义.)

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.)

这篇关于Scala中的LINQ类似物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆