score:27
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 ienumerable
s.
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 -
- database accesses
- asynchronous programm flow
- maybe-monads
- list comprehensions
- recursive descent parsers
- continuations
- mini-languages
- parallel computations (plinq)
some links:
- list of linq providers
- marvels of monads
- linq or language integrated monads
- linq parser library
- another explanation of monads (f#)
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?
Source: stackoverflow.com
Related Query
- How much is there to LINQ?
- How much interoperability is there between C++ and Scala?
- Is there a way to check how much data is loaded into a buffer?
- Is there someway to limit how much rows a join will catch in Spark?
- Is there something like LINQ for Java?
- How many implicits are there in Scala?
- Is there a simple example of how to generate verilog from Chisel3 module?
- How much of a jar file gets loaded into the memory
- In Scala or Java, how to get how much RAM does application currently occupy?
- How do you call this kind of concurrency related class? And is there a standard implementation?
- Is there any comprehensive tutorial on how to use this kind of structural typing?
- How much optimization does Scala's pattern matcher perform?
- How to specify a generic function that works as if there is a supertype of Int, Double, etc?
- Why does the same algorithm work in Scala much slower than in C#? And how to make it faster?
- Tweet value prediction: What sort of analysis (Bayesian?) might predict how much a Twitter user will value a tweet?
- Using Scala how to find out if a list of chars are present in a String, where there are duplicate characters?
- Show how much time the compiler spends on type inference
- how many ways are there to add a new column to a data frame RDD in Spark API?
- Is there an example of how to use akka-http with servlet container?
- In Scala 2.8, how to write (append) a line to a file? Should I use Java clesses or there are native Scala functions for this?
- How to failed compilation if there are any dead code or unused imports, variables etc
- How much memory does Array[Byte] occupy in Scala?
- Is there a Scala function of type `Nothing => A`? Or how to construct one?
- Scala Reflection: How to find a no-arg constructor via reflection (if there are multiple constructors) ? What is the type signature of "no-arg"?
- Alpakka : how to deserialize xml to objects - Is there a more concise pattern for deserializers?
- How to avoid holding too much logs in `Writer Monad`?
- Is there any demo to show how to use the placeholder like this in Scala?
- `zipWithIndex` or not: how much do we gain?
- Experiences with Scala swing, how does it add to what's already available in Java swing? Are there any challenges?
- How much processing should be done in an Akka actor ?
More Query from same tag
- Installing sbteclipse
- Finch configure response to non existing endpoint
- Delete Unicode value in output of Spark 1.6 using Scala
- Spark Scala how to process multiple columns in single loop
- kafka api: java.io.ioexception: can't resolve address: yyy.x.x.xx:9091
- how to create a connection between scala and mysql using jdbc
- 'Graph must be connected' with Flow created from Publisher+Subscriber actor
- troubleshooting kafka + flink example using scala sbt?
- Bulk insertion of data to elasticsearch via logstash with scala
- Questions about a la carte data types
- Scala isInstanceOf[T] function fail to use bounded ClassTag/TypeTag
- Scala type mismatch error even after checking type with isInstanceOf
- Strange pattern matching behaviour with AnyRef
- When should I use Option.empty[A] and when should I use None in Scala?
- Scala recursive types
- sbt assembly error:deduplicate: different file contents found in io.netty.versions.properties
- Create Akka Dispatcher programmatically
- Compound type vs mixins in scala
- I have two sets of string,I want get result of combination
- Re-use string that are using on pattern/matched/replace/split Spark function
- http4s json handling in authed routes
- Akka Streams how to write a GraphStage with OrElse
- How can I have a typed (specific) factory in a base trait in Scala?
- Make ScalaFx working on both JDK 8 and 11
- reduce a sequence with two types to a sequence with one
- How get generic parameter in scala
- Finagle filter sequence of futures of option
- How can I initialize a dense matrix with prepared matrix-like data
- What is the meaning or use of { _ => } and _ = somefunctionname() in scala
- How do I sort a collection of Lists in lexicographic order in Scala?