有多少是那里LINQ? [英] How much is there to LINQ?

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

问题描述

我寻找到LINQ,并出现查询语言(至少在表面上),以只不过是地图和/或列表COM prehensions的实现更是在Haskell和其他FP语言中(特别是在Scala的地图'和'为')的推广。它是否正确?是否有更多的语法比这个?从书中我读(必要LINQ)的基调气喘吁吁这似乎像有一些新的或创新在这里。

I'm looking into LINQ and the query language appears (at least on the surface) to be nothing more than an implementation of map and/or list comprehensions as found in Haskell and other FP languages (particularly the generalisation of 'map' and 'for' in Scala). Is this correct? Is there more to the syntax than this? From the breathless tone of the book I'm reading ("Essential LINQ") it would seem like there's something new or innovative here.

有是整个后端,管道,一阶前pression树木和类型等来实现LINQ,但我的问题是有关查询语言本身。

There's the whole back-end, pipeline, first-order expression trees and types etc to implement LINQ but my question is about the query language itself.

干杯

推荐答案

在功能上说,LINQ不过是前pressing单子的句法简化。 LINQ到对象(名单-COM prehensions - 即使这会已经是非常有用的),你一直在讲,就是一个可能的的这个应用程序(类似于清单,单子中哈斯克尔)。

Functionally spoken, LINQ is nothing but a syntactic simplification of expressing monads. Linq to Objects (List-comprehensions - even this would already be extremely useful), which you have been talking about, is just one possible application of this (similar to the List-Monad in Haskell).

如果你写

from x in expr1
from y in expr2
select x + y

这只不过

do
    x <- expr1
    y <- expr2
    return $ x + y

在Haskell。

in Haskell.

这是做具体的事情取决于用户定义的的LINQ提供程序的(扩展的方法),其中 Linq.Enumerable 只是一个涉及实施的IEnumerable 秒。

The concrete thing that is done depends on user-defined Linq-providers (Extension-Methods) of which Linq.Enumerable is just one implementation involving IEnumerables.

通过提供一个,你可以为你的类型创建全新的LINQ的语义。

By providing one, you can create completely new LINQ-semantics for your types.

例如:给定一个选项键入可能失败(空的值)的计算,我们可以定义一个LINQ提供者进行查询过他们。

Example: Given an Option type for computations that may fail (nullable values), one could define a Linq-provider for querying over them.

public static class MaybeExtensions
{
public static Option<T> ToMaybe<T>(this T value)
{
    return Option<T>.Some(value);
}

public static Option<U> SelectMany<T, U>(
    this Option<T> m, 
    Func<T, Option<U>> k)
{
    return !m.IsNone ? Option<U>.None : k(m.Value);
}

public static Option<V> SelectMany<T, U, V>(
    this Option<T> m, 
    Func<T, Option<U>> k, 
    Func<T, U, V> s)
{
    return m.SelectMany(x => k(x).SelectMany(y => s(x, y).ToMaybe()));
}
}

现在,这将使我们能够写出这样code:

This would now allow us to write such code:

var sum = from x in ReadNumber("x")
          from y in ReadNumber("y")
          select x + y;

的计算只会返回一个值,如果所有的计算成功,并在第一个失败,否则将失败。

The computation will only return a value if all computations succeeded and will otherwise fail at the first failing one.

在与前pression树木组合,LINQ的可能非常强大,可以让你的前preSS -

In combination with expression trees, Linq can be extremely powerful and allows you to express -


  1. 数据库访问

  2. 异步PROGRAMM流程

  3. 也许-单子

  4. 列表COM prehensions

  5. 递归下降解析器

  6. 延续

  7. 迷你语言

  8. 并行计算(PLINQ)

某些链接:

  • List of linq providers
  • Marvels of monads
  • Linq or language integrated monads
  • Linq parser library
  • Another explanation of monads (F#)

中的LINQ提供了一个完整的功能迷你语言(的 Linq的光线跟踪)。

Combined with fixed-point combinators, Linq provides a complete functional mini-language (Linq raytracer).

注意,Scala和F#都有在泡沫prehensions和计算前pressions类似的概念均为一元抽象:

Note that Scala and F# both have similar concepts in for-comprehensions and computation expressions both being monadic abstractions:

斯卡拉:

for (x <- expr1
     y <- expr2) yield x + y

F#

monad {
    let! x = expr1
    let! y = expr2
    return x + y
}

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

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