Wednesday, January 07, 2009

TIOBE language of the year not Delphi

In october Jim Mckeeth reported that Delphi could well be the language of the year in the TIOBE Community language index.
Unfortunately this has not happened despite the efforts of the Delphi community putting "Delphi programming" on webpages.

In january Delphi is fallen slightly one place back to position 10. (Nothing to worry about). Remarkable is that Pascal went up 3 places compared with last year, now holding the 15th position. I predict that Pascal will become the language of the year 2009! ;-)

Language of the year 2008: C

For what it is worth........

What is your prediction?

Tuesday, January 06, 2009

LINQ with Delphi Prism #2 : Deferred Execution

As said in this previous post Delphi Prism supports LINQ all the way so all the LINQ feature just work in Delphi Prism.

Deferred Execution
One of the key features of LINQ is Deferred Execution. That means that a LINQ query does not execute until data is demanded, for except by an iteration through a list. This makes it possible to make a complex query without execution each part of the query seperately.

However, there is one exceptions to the rule, a LINQ query executes immediately when you use a function that must iterate over the list for its result like, for example Sum(), Count(), ToList() etc.

//Example of deferred execution or lazy execution
var Numbers := new List<integer>;
var DoubledList : sequence of integer
= Numbers.Select(n->n*2);
// Query not yet executed!
// Add another number to the list

//At this point query is executed
//Result "2--4"
for i : Integer in DoubledList do begin
Result :
= Result + i.ToString + '--';

//Example of direct execution
var Numbers := new List<integer>;
//Query executed here
var Sum := (Numbers.Select(n->n*2)).Sum();
//Would expect 20, but result is only 6

Monday, January 05, 2009

LINQ with Delphi Prism #1 : Sequences

I have been digging into LINQ this christmas. LINQ is a cool way to query data, whether it is an array, a list or a remote datasource.
LINQ is a new feature of C# 3.0 that comes with Visual Studio 2008, but guess what, Delphi Prism, also supports LINQ all the way.

If you want to play/test LINQ queries syntax you can download and install LinqPad, a free tool to use LINQ querys against a SQL Server database. (You could drop Management Studio for that).

The only disadvantage I found (IMO) on LINQ so far is that quering remote datasources is bound to Microsoft SQL Server database. (Maybe this will be extended in the future)
That involves, the so called "LINQ to SQL" methods, which is, I believe, allready deprecated.

As said before Delphi Prism completely supports LINQ. Starting a 3.5 framework application will give you a reference to System.Linq, the namespace, where LINQ lives.

In this series I will explore LINQ using Delphi Prism:

1. Using LINQ on a sequence
LINQ, in Delphi Prism, works on so called sequences, which is a new type described in the Delphi Prism Wiki as:

Sequences are a special type in the language and can be thought of as a collection of elements, similar to an array.

Declare a sequence like this:

Names : sequence
of String :=
'Mickey', 'Dick', 'Roland', 'Delphi', 'Harry'];

With LINQ it is very easy to query a collection, like this sequence. You can do this in two ways, namely:
1. Using Lambda expressions
2. Using query comprehension syntax (Query Syntax)

With Lambda expressions you can write rather small expressions to query the data, with the query syntax they become more readable (read less magic ;-)).
By the way the compiler (at least in C#) will translate the query syntax into lambda expressions. Both techniques are complementary. (I think the Oxygene compiler also does this....)

Suppose we want to have all the names from our sequence which have more then 4 characters, containing an "a" and sorted in upper case.

//Using Lambda syntax
var FilteredNames := Names
.Where (n
-> (n.Length >= 4)
and n.Contains('a'))
.OrderBy (n
-> n)
.Select (n
-> n.ToUpper());

//Using query comprehension syntax
var FilteredNames := from n in Names
where ((n.Length
>= 4)
and n.Contains('a'))
order by n
select n.ToUpper;

//Note we don't have to declare FilteredNames

Iterate through the filterednames to show them in a messagebox:

for s : string in FilteredNames do begin
FilteredOnes :
= FilteredOnes + s + "--"

'FilteredOnes: ' + FilteredOnes)

Note that the original collection, Names in this case, is still holding all the elements.

Conclusion LINQ is a, in basic, simple way to query collections the SQL way. Delphi Prism supports them complete. Choosing Lambda expression or Query syntax will be mostly a personal preference.