score:27

Accepted answer

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

if you write

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

it's nothing but

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

in haskell.

the concrete thing that is done depends on user-defined linq-providers (extension-methods) of which linq.enumerable is just one implementation involving ienumerables.

by providing one, you can create completely new linq-semantics for your types.

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.

in combination with expression trees, linq can be extremely powerful and allows you to express -

  1. database accesses
  2. asynchronous programm flow
  3. maybe-monads
  4. list comprehensions
  5. recursive descent parsers
  6. continuations
  7. mini-languages
  8. parallel computations (plinq)

some links:

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

note that scala and f# both have similar concepts in for-comprehensions and computation expressions both being monadic abstractions:

scala:

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

f#:

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

score:2

linq was inspired by haskelldb, as erik meijer has numerously stated, e.g. in confessions of a used programming language salesman (getting the masses hooked on haskell), so it is not in itself a new concept. using the same language to query different sources is to some extent innovative, although the fact that nested-relational model covers xml, objects, and relational databases has been shown by researchers before. for me, what's extremely cool is that it has been embedded into a popular, general-purpose, and primarily object-oriented language, which hasn't been done before.

scala imho has the capacities to incorporate something similar. so far, for scala we have stefan zeiger's scalaquery and daniel spiewak's scalaql, that follow linq footsteps.

score:3

the core of linq, the query syntax, isn't actually huge in scope. it is simply some very literal translations, to methods and lambdas - so

var qry = from x in src
          where x.foo == "foo"
          select x.bar;

is literally:

var qry = src.where(x => x.foo == "foo").select(x => x.bar);

it knows nothing about extension methods (although they are the most common (but not only) implementation), and nothing about expression etc. the number of keywords (and hence the number of required method implementations) isn't huge. jon once attempted to implement all of them in 1 hour (in a live presentation). he didn't do too badly ;-p


perhaps the more impressive part of linq is the expression tree support that was required to allow linq to be used against databases - i.e. the lambda expression that can be compiled either to a delegate or to an object model that represents the code written. interestingly, this same idea shines through into the way that dlr resolution works in 4.0.

score:4

besides reading a book about it, did you already used linq? i found it to be a huge timesaver in my daily programming work. for me, it's the next step of abstraction, which can be used to combine different datasources like xml or sql and working with them in same "language".

furthermore, i recommend this interview with anders hejlsberg about functional programming and linq.

score:5

the breathlessness is probably intended for all that "obvious" stuff, some of which (like expression trees) is truly excellent. the language is just a means of access; do you get excited over the throw keyword, or over the functionality it exposes?


Related Query

More Query from same tag