LINQ preview and Lisp

I really wanted to be excited about .Net 2.0 and C#’s 3.0 LINQ preview. However, between .Net1.1 and .Net2 coming out, I started to play with LISP.

First, I was digging through some C# 2.0 the other day (now with Generics and “Anonymous Delegates Outer variable capture” (craptastic, hacked, statically typed closures)), and while reading a blogpost (that I lost, sorry) the blogger maid the excellent point that in removing static typing from some things (generics) they had to add reams of code. Something there should smell bad already. If you find yourself adding reams of code (and more, new syntax) to get away with leaving out half of what the language originally made you specify, maybe the language just shouldn’t force you to specify that info. I dunno maybe I’m crazy and/or stupid and/or a type-ist?(someone who’s against typing?).

On to LINQ: When I saw this forthcoming tech, LINQ, I thought, “Hey relational algebra-ish support inside of the language, nice”. Then I looked at the demo and the only thing I can think of is all of the lisp constructs to do this sort of high end querying. All of the LISP ways that were built into the language long before its standardization (ANSI standardization: December 8, 1994) a decade ago. I am fairly sure that Ruby blocks can be put to use to accomplish this same sort of thing. Also Python list comprehensions. It really makes me question why people are willing to wait another 2-3 years for LINQ when they are a download away from better, more dynamic frameworks. Oh… right… its next to impossible to talk to sql server outside of an MS environment (vendor lockin). And as always learning anything new (that isnt ms )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C# Linq Demo from this .net blog
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
XDocument doc = XDocument.Load(“blog.xml”);

var d = new Dictionary();
d.Add(“2005/09″,”September 2005”);
d.Add(“2005/08″,”August 2005”);

var a = new string[] { “greasemonkey”, “ajax” };

var query =
from
item in doc.Descendants(“item”),
key in d.Keys,
tag in a
where item.Element(“date”).Value.Contains(key) &&
item.Element(“tags”).Value.Contains(tag)
orderby
(string) item.Element(“date”) descending
select new XElement(“item”,
new XElement(“month”,d[key]),
item.Element(“date”),
item.Element(“title”),
item.Element(“tags”));

foreach (var result in query)
Console.WriteLine(result);

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lisp with Loop which as far as I can tell is about as close as I can get LINQ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(mapc
(lambda (elem) (print elem))

(loop for item in doc.Decendants(“items”)
for key in d.Keys
for tag in a
when (and
item
(find key (value (item.Element “date”)))
(find tag (value (item.Element “tags”))))
collect (make-instance ‘XElement
:tag-name “month”
:children (list (make-instance ‘XElement :tag-name “month”)
(item.Element “date”)
(item.Element “title”)
(item.Element “tags”)))
into new-elems
unless item do (return (sort new-rows #’xml-item-date-< )))

There might be more that I am missing (for example an XML library (use ClosureXML), and a predicate that will work for the sorting he wants). About the nicest part of the LINQ stuff seems to be the OrderBy clause (also possibly GroupBy which isnt covered in the demo, but I could see being cool). However, sorting is more or less a done job given an appropriate predicate. Also there might be slightly more involved iterating going on in the query, but nothing that couldnt be handled with a modicum more code, wrapped in a macro to give it that linq-ish look, and packaged up to be useful in a week.