[fpc-devel] New feature discussion: for-in loop
Graeme Geldenhuys
graemeg.lists at gmail.com
Tue Oct 20 09:57:18 CEST 2009
2009/10/20 Paul Ishenin <ip at kmiac.ru>:
>
> It is obvious you are full of energy to discuss new language features.
:-)
>
> The next feature which we should think of is for-in loop:
> http://wiki.lazarus.freepascal.org/for-in_loop
As an alternative to the very limited "for-in", I have a better and
more flexible design, using the Iterator design pattern. I have
already written iterations for most of FPC's list components. It would
be nice if the iterators could be part of the default list component
and not as an external unit like I currently have. I'm willing to do
the work in merging my external iterators with the various list
components. Just say the word.
It allows you to write code as follows, irrespective of what list
component you are working with.
MyIterator := MyList.GetIterator;
while MyIterator.HasNext do
ShowMessage(MyIterator.Next);
The iterator interfaces has a lot more functions that just .HasNext and .Next.
For example, I have a String Iterator that use regular expressions to
"filter" the return results. So now you have the exact same code as
above, but ShowMessage() will only act on the filtered return results.
As you can see, a lot more flexible that the for-in construct.
Additional methods that the iterator can support.
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)
BTW: Java and .NET have such type of iterators on just about every
list component and it's very handy (I can only comment here on Java
implementation because I don't use .NET).
I have written a article about this - in a lot more details. You can
get a copy of the article at the following url:
http://opensoft.homeip.net/articles/
--
Regards,
- Graeme -
_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
More information about the fpc-devel
mailing list