A
agent_008_nl
Guest
My present of today on the subject of the (oo, gof) iterator design pattern (reference below, excerpt above). G. WHY USE MAPPERS INSTEAD OF ITERATORS? The major problem with iterators is that they are non-functional functions--they depend upon an internal state and side-effects to operate. It is very difficult to prove anything about functions with state, and therefore such functions inhibit most compiler optimizations. But the most damning criticism of iterators occurs in the context of multitasking and parallel threads. When using parallel threads, all side-effects must be protected by locks, in order to ensure the consistency of interpretation of the contents of shared objects with state. While most iterators in C++ are locally-defined for a particular loop, and therefore not shared with any parallel process, it is difficult for a compiler to prove this. As a result, one is either left with an inefficient program, or a potentially dangerous program, depending upon whether the compiler inserts locks to be on the safe side, or leaves them out in the name of efficiency. These problems do not arise with the higher-order mapping functions. As we have shown, mapping functions can be completely functional (i.e., they have no shared state or assignments), even for complex iterations like those for comparing the fringes of non-isomorphic trees. This means that an arbitrary number of mappers can transparently access the same collections without interference--at least so long as the collections are not updated. If the collections are also read-only, or functional, then there is no possibility of interference under any circumstances. Mappers create function closures during their operation, and these temporary objects hold the "state" of the iteration, much as iterator objects do in C++. The difference is that these closure objects are created automatically during the execution of the mapping functions, rather than having to be explicitly created by the programmer, as in C++, and the side-effect-free nature of mappers is obvious to a compiler based on a cursory syntactic scan. --------------------------- www.pipeline.com/.../Iterator.html
Continue reading...
Continue reading...