[fpc-pascal] a few trivial questions
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:
itr := gIteratorFactory.StringIterator(sl);
while itr.HasNext do
I have created Iterators for all existing list types and the code (and
accompanied article explaining Iterators) are freely available at:
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
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....
...Michael answered the rest of the questions.
- Graeme -
fpGUI - a cross-platform Free Pascal GUI toolkit
More information about the fpc-pascal