Iteration classes for string, range, object instance, collection and custom object
In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
While ($it.hasNext())
$aValue:=$it.next()
// do something
End while
You can iterate over different type using the same way
Iterate over a range of numeric
$it:=it .Range.new(1;7)
While ($it.hasNext())
$value:=$it.next() //1, then 2, ..., until 7
End while
it .Range.new(1;7).toCollection() // New collection(1;2;3;4;5;6;7)
Iterate over string characters, transform it to collection
it .StringIterator.new("Hello World").toCollection() // New collection("H";"e";"l";"l";"o";" ";"W";"o";"r";"l";"d")
eq. of [..."Hello World"]
in some languages
There is different type of iterator provided, to traverse a collection, an object keys, or values or entries or even custom algorithms.
Type | Description |
---|---|
it.CollectionIterator | Iterate over a collection |
it.SelectionIterator | Iterate over a selection |
it.ObjectEntryIterator | Iterate over an object entries collection |
it.ObjectKeyIterator | Iterate over an object keys collection |
it.ObjectValueIterator | Iterate over an object values collection |
See some usage in tests
Type | Description |
---|---|
it.IterableIterator | Iterate on object which implements two formula: length()->int and getValueAt(index)->value |
You can use pass an iterable to PROCESS 4D TAGS
, the template will be easier to read
<!--#4DLOOP ($1.hasNext())-->
<!--#4DEVAL $value:=$1.next()-->
<!--#4DENDLOOP-->
If a class have a function iterator
which return an Iterator
instance, we could imagine For each
language keyword work with it, instead of keys browsing