LINQ 多少钱? [英] How much is there to LINQ?

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

问题描述

我正在研究 LINQ 并且查询语言似乎(至少在表面上)只不过是在 Haskell 和其他 FP 语言中发现的地图和/或列表推导式的实现(特别是地图"的泛化' 和 'for' 在 Scala 中).这个对吗?还有比这更多的语法吗?从我正在阅读的这本书(Essential 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.

有整个后端、管道、一阶表达式树和类型等来实现 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 只不过是表达 monad 的句法简化.您一直在谈论的 Linq to Objects(列表推导式 - 即使这已经非常有用)只是 一种可能的应用程序(类似于 Haskell 中的 List-Monad).

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-providers(扩展方法),其中Linq.Enumerable只是涉及IEnumerable的一个实现s.

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.

示例:给定一个 Option 类型的计算可能会失败(可为空值),我们可以定义一个 Linq-provider 来查询它们.

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()));
}
} 

这将允许我们编写这样的代码:

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.

结合表达式树,Linq 可以非常强大并允许您表达 -

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

  1. 数据库访问
  2. 异步程序流程
  3. Maybe-Monads
  4. 列表推导式
  5. 递归下降解析器
  6. 续篇
  7. 迷你语言
  8. 并行计算 (PLinq)

一些链接:

结合定点组合器,Linq 提供了一个完整的函数式迷你语言(Linq 光线追踪器).

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

请注意,Scala 和 F# 在 for-comprehensions 和计算表达式中都有相似的概念,它们都是一元抽象:

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天全站免登陆