DeHL 0.4: Linq-ifying Delphi

Since I have started working on DeHL, one of the main purposes was to get to the point where I could use Linq-like extensions on collection classes. Of course I could not call the new functionality Linq since it’s not language-integrated, so I decided to give it another name: Enex (aka Enumerable Extensions). All collection classes in DeHL now support a wide set of methods that allow querying and selecting elements from the starting collection. A lot of work has been put into this release, ranging from extensions to type system, object life-time management, optimizations and bug-fixes and of course a lot of testing.

Now, back to Enex, let’s visit some cool examples of what the new functionality can do:

Let’s check a very simple example. Assuming you have a list of integers and you want to find the sum of all elements that are greater than the list average. In the “times before” you wold write something like this:

  { Calculate the average of the list }
  Sum := 0;

  for I := 0 to List.Count - 1 do
    Sum := Sum + List[I];

  Avg := Sum div List.Count;

  { Calculate the sum of the elements above average }
  Sum := 0;

  for I := 0 to List.Count - 1 do
    if List[I] > Avg then
      Sum := Sum + List[I];

… Now it is as simple as this:

  { ... }
  Sum := List.WhereGreater(List.Average()).Sum();
  { ... }

Now, let’s see another more complex example. Considering that you have two collections of integers. You have to determine the maximum value common to both collections. Check it out:

  { ... }
  Max := AColl1.Intersect(AColl2).Max();
  { ... }

Next: what is the sum of all odd numbers in interval [0..100]?

  Sum := TEnexEnumerable<Integer>
    .Interval(0, 100)
    .Where(function(Arg1: Integer): Boolean
      begin
        Exit(Odd(Arg1);
      end)
    .Sum();

The last one: You want to take all integers from a list with their absolute value and only once; and you want to write them on the screen directly:

  for I in ARandomList
    .Select(function(Arg1: Integer): Integer
      begin
        Exit(Abs(Arg1));
      end)
    .Distinct()
  do
    WriteLn(I);

What’s working and what’s not:

  • Some problems in the compiler prevent the use of generic function Select<TOut> and Cast<TOut> for now. These two could have been used to transform each element of the collection to some other type.
  • Everything is based in interfaces to make use of automatical garbage collection. the only restriction is that the first enumerable (from which the chain starts) should be either an interface or must be destroyed explicitly.
  • All collections have overriden versions of some functions. For example in a list, the First() method is implemeted directly so that you benefit from full speed whenever possible.
  • No lambdas … that makes writing selectors or predicates rather unpleasant.
  • Sum() and Average() are only supported for types registered as integers or reals (including BigCardinal and BigInteger).
  • All selector and predicate-based selection is done on-the fly so there is no additional memory used!

There are of course much more possible use cases for Enex-enabled collections, I simply do not have the time to list all of them.

You can download the latest version of DeHL here.

9 Responses to 'DeHL 0.4: Linq-ifying Delphi'

  1. Zenberg says:

    I really like DeHL, but to be honest, I don’t like this direction of DeHL, because you can not enum the requirements of developers when the use Collections.

    Things like ‘List.WhereGreater’ would be inflated out of control.

    When we want add custom algorithms to collections, maybe STL’s way is better.

  2. Markus says:

    The name of the author of this blog is not shown on CG’s blog website. Can this be changed?

  3. alex says:

    @Zenberg:

    List.WhereGreater was added simply because the IType has this knowledge. There won’t be more methods than this exposed. That is it.

    Enex was added primarily to resolve the need for some fast, predefined operations and nothing more. Also the best part is that it doesn’t affect the speed of the collections.

    And yes, we’re planning to add STL-like style of math programming.

    @Markus:
    This happens because the blog is hosted on another site (this one) and EMBT’s one simply aggregates the content there.

  4. Wonderful website you have here but I was curious about if
    you knw of any forums that cover the same topics
    talked about here? I’d really like to be a part of group
    where I can get responses from other experienced
    people that share the same interest. If you have any suggestions, please let me
    know. Thanks a lot!

  5. Berniece says:

    When someone writes an piece of writing he/she retains the plan of a user in his/her mind that how a user can know it.
    So that’s why this piece of wrriting is amazing.

    Thanks!

  6. I am now not certain the place you are getting your info,
    but great topic. I must spend some time learning more or figuring out more.
    Thank you for fantastic info I used to be on the lookout for this information for my
    mission.

  7. It’s remarkable to pay a quick visit this website and reading the views of all
    colleagues regarding this piece of writing, while I am also keen of getting knowledge.

Trackbacks/Pingbacks
  1. […] roots!   Heute, 09:30 Zitat: * Spracherweiterungen (LINQ) Get teilweise schon, dank DeHL Markus […]

  2. Anonymous says:

    […]   Heute, 15:31 Was ist denn mit DeHL? Ich habe es noch nicht ausprobiert, aber es liest sich zumindest interessant. […]

Leave a Reply

Your email address will not be published.