What
I'd like to see iterators for lists and maps and maybe even arrays in PB implemented on two levels.
Level 1:
Code: Select all
CreateIterator(NumberOrPBAny.i, List())
*ElementData = IteratorNext(Number.i)
; or
IteratorNext(Number.i, @*ElementData) ; such that While IteratorNext(..., @*ElementData) is possible and there's no need for more lines.
ReleaseIterator(Number.i)
Code: Select all
ForEach [*ElementData[.Type] In] List()
; do something with *ElementData
Next
Code: Select all
EnableExplicit
Macro ForEachList(name, type, container)
Define name.type
ResetList(container)
While NextElement(container)
name = @container
EndMacro
Macro ForEachListNext(name, container)
ChangeCurrentElement(container, name)
Wend
EndMacro
NewList X.i()
AddElement(X()) : X() = 1
AddElement(X()) : X() = 2
AddElement(X()) : X() = 3
Debug "Classic ForEach cannot be nested easily (without storing current position) as you can see here:"
ForEach X()
ForEach X()
Debug X()
Next
Next
Debug ""
Debug "ForEachList can be nested"
ForEachList(*X1, Integer, X())
ForEachList(*X2, Integer, X())
Debug *X2\i
ForEachListNext(*X2, X())
ForEachListNext(*X1, X())
Code: Select all
Classic ForEach cannot be nested easily (without storing current position) as you can see here:
1
2
3
ForEachList can be nested
1
2
3
1
2
3
1
2
3
Why
Whenever I come back to PureBasic I struggle with that, since nesting is not always so obvious. Imagine something like this:
Code: Select all
ForEach X()
Subroutine(X())
Next
That's actually why I wanted the C headers for user libraries. I saw a PB_List thing there.