[Lazarus] Easiest way to "case" strings

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Mar 26 13:12:26 CET 2009


On Thu, Mar 26, 2009 at 12:29 PM, Alexander Klenin <klenin at gmail.com> wrote:
>
>> And I assume that by "lack of iterators" you mean in-language iterators?
> Of course. What else?

External iterators without the need for implementing descendants. But
I must agree, it would really be nice if we can incorporate iterators
in all list/container classes.

Would the FPC developers accept such a patch?  With unit tests of
coarse. :-)  It will not break anything, because it never existed
before - not in Delphi or FPC.  But it would make FPC classes a lot
more user friendly.

> versus
>
> var
>  item: TItem;
> ...
> for item in obj.Items do begin
>  ... item ... item ...
> end;
>
> the latter is safer, simpler and more clear code.


var
  itr: ITBStringIterator;
begin
  itr := gIteratorFactory..StringIterator(MyStringList);
  while MyStringIterator.HasNext do
    writeln(itr.Next); //  .Next does not increment the iterator.
    ...
    // do something else with itr.Next
  end;
  // itr gets free'ed automatically for us...
end;


We can even have filtered iterators using regular expressions. You can
iterate over certain items only be that files in a diretory or strings
in a TStringList etc...

fitr := gIteratorFactory.FilteredStringIterator(MyStringsCollection, 'foob.*r');
while fitr.HasNext do
  DoSomethingWithItem(fitr.Next);


Here is an example of the supported iterator API I have implemented.

Function                        Description
===============================================================
Add(item)         Inserts a specified item into the collection.
                  (optional modifier operation)
HasNext()         Returns true if the collection has more items
                  when traversing the collection in the forward
                  direction.
HasPrevious()     Returns true if the collection has more items
                  when traversing the collection in the reverse
                  direction.
Next()            Returns the next item in the collection.
Previous()        Returns the previous item in the collection.
Reset()           Jump to the beginning of the collection, setting
                  the cursor/index before the first item in the
                  collection. The iterator is now in the same state
                  as if you just created it.
ToBack()          Jump to the end off the collection, setting
                  the cursor/index after the last item in the
                  collection. This needs to be called before you
                  want to traverse the collection in the reverse
                  order.
PeekNext()        Returns the next item without moving the
                  iterator's cursor/index.
PeekPrevious()    Returns the previous item without moving the
                  iterator's cursor/index.
Remove()          Removes from the collection the last item that
                  was returned by the Next() or Previous() calls.
                  (optional modifier operation)
SetItem(item)     Replaces the last item returned by the Next()
                  or Previous() calls with the specified item.
                  (optional modifier operation)
-------------------------------------


A *huge* benefit of using iterators, is that the developers doesn't
need to worry about if the list/container is 0-based or 1-based, how
to get the next or previous item etc... It now gives the developer a
consistent interface API to work with, no matter what is being
iterated.


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/




More information about the Lazarus mailing list