[fpc-pascal] a few trivial questions

Graeme Geldenhuys graemeg.lists at gmail.com
Wed May 12 17:19:57 CEST 2010


2010/5/12 spir ☣:
>
> * TFPList
> Is there another way to traverse a list than
>    for i :=0 to (list.count - 1) do ...
> What about list.high?

Yes, I use the Iterator design pattern. This allows me to write code as follows:

---------------
var
  itr: ITBStringIterator;
begin
  ...
  itr := gIteratorFactory.StringIterator(sl);
  while itr.HasNext do
     writeln(itr.Next);
   ...
end;
---------------

I have created Iterators for all existing list types and the code (and
accompanied article explaining Iterators) are freely available at:
http://opensoft.homeip.net/articles/

Iterators have the added flexibility that I can even combine them with
regular expressions to creating a filtered list.
-----------
fitr := gIteratorFactory.FilteredStringIterator(MyStringsCollection, 'foob.*r');
while fitr.HasNext do
begin
  DoSomethingWithItem(fitr.Next);
  ...
end;
-----------

I can also move backwards, forwards, peak back or forward, reverse the
iterating, reset the iteration etc. Iterators are very flexible.


> Ok, I can write these funcs (I did ;-). But how comes there are no builtin functions for that?

IntToStr(), Format(), FormatFloat() etc...?


> Is there another way to slice a string than using copy. In particuliar, the count argument is not always practical to indicate the end_of_section position. (count = last-first+1)
>

Again here you can use iterators to iterator character by character if
you want. No need to know about Count or Count-1 etc. For all types of
lists you now have the same interface....
    iterator.HasNext


...Michael answered the rest of the questions.


-- 
Regards,
  - Graeme -


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



More information about the fpc-pascal mailing list