KBD

Keith Devens .com

Tuesday, December 2, 2008 Flag waving
The Lord is my shepherd; I shall not want. – David (Psalm 23)

Archive: September 18, 2005

← September 17, 2005September 19, 2005 →

Daily link icon Sunday, September 18, 2005

Anders Hejlsberg on C# 3.0

Slashdot | Anders Hejlsberg on C# 3.0:

The new language enhancements include implicitly typed locals, extension methods, strongly-typed lambda expressions, anonymous types, and LINQ - a builtin SQL-like syntax for data access.

Rock on. I recently saw an article on LINQ at LtU, but the technology didn't pass my importance filter so I didn't read it. Now I find it's going to be in C#. But given my cursory look at some of the samples /. linked to, I don't immediately see how it's much different than Python's list comprehensions.

For instance, in Dan Fernandez' blog post about LINQ linked from the page with the interview with Anders Hejlsberg, he uses these examples:

 var result =
   from s in aBunchOfWords // query the string array
   where s.Length == 5     // for all words with length = 5
   select s;               // and return the string

 var result =
   from c in allCustomers
   where c.ContactTitle.Length == 5
   select c.ContactName;

I don't see how that's any different than Python's:

result = [s for s in aBunchOfWords if len(s) == 5]
result = [c.ContactName for c in allCustomers if len(c.ContactTitle) == 5]

And I think Python's syntax is far more elegant. Of course, allCustomers in the second example was actually a database table. But you can already do that easily with Python's (or any other language's) generators. Consider (from an interactive console session):

>>> def allCustomers():
...    custs = ["larry", "curly", "moe"]
...    for cust in custs:
...       yield cust
...
>>> [c for c in allCustomers() if len(c) == 5]
['larry', 'curly']

Of course, the syntax is a little different between the two examples, so a little bit of the implementation details leak into the syntax. Nothing's perfect.

Update: Slava on C# 3.0:

Take a look at the features planned for C# 3.0; they're basically adding a bunch of features from Common Lisp and ML... For years we have heard Java advocates say that higher order functions and expression trees lead to complex code that is hard to understand, despite plenty of evidence to the contrary. But now that Microsoft is adding these features, watch Sun scramble to catch up with Microsoft, yet again!

Update: Now I know what they mean by "implicitly typed locals". About 5:30 into the video Hejlsberg explains it (calling it "local variable type inference"). You can say var c = customers[i] instead of Customer c = customers[i] or Customer c = new Customer(). I've always complained that the latter two have redundancy, so I'm definitely a fan of that new feature. Plus, does this mark the first entrance of type inference (basic though it is) into a mainstream language?

Update: After watching more of the video, that query language is pretty cool. I love how it works with anything that's IEnumerable, and that the expressions given as filters are actually type-safe (with type inference) lambas, creating anonymous types as they go.

← September 17, 2005September 19, 2005 →
December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

new⇒Perl 6 1.0 in March?

Doh, my mistake. I'm aware of the​relation between Parrot and Rakudo​but I'...

Keith: Dec 2, 1:03am

Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

WhatIsMyIP.com

http://www.thesysteminfo.com is​another good alternate to​whatismp.com... I...

Kripz: Nov 26, 8:51pm

Girls, please don't get breast implants

Actually I think it's sweet when a​man loves a woman whether she's big​or n...

218.186.12.228: Nov 26, 9:40am

Generated in about 0.062s.

(Used 7 db queries)

mobile phone