From: Ben Tilly <bti...@gmail.com> Date: Wed, 10 Apr 2013 14:09:19 -0700
On Wed, Apr 10, 2013 at 1:53 PM, Bob Rogers <rogers-...@rgrjr.dyndns.org> wrote: > I would hope (but don't know how to check) that Perl is smart enough to > notice that the "map" in my original example is in a void context, and > won't bother to store any return values from the block . . . Current versions of Perl are sufficiently smart to not build a return list. You can verify by creating objects with destructors inside the map and seeing when they get destroyed. Good idea; I doubt I would have thought of that. > Over the years, I have seen many applications for mapping over trees > (and also written a few), and most of them just produce side effects, > either to the tree or externally. Those that don't tend to be of the > nature "collect (or count) all nodes that satisfy X," for which a > non-local exit would defeat the purpose. Those that do require > non-local exit are of the form "find the *first* X" or "return true if > *any* X," where the burden of handling a scalar return is minimal and > the benefit from being able to skip the remainder of the tree is clear. Many also are of the form, "Start where you left off, give me the next one." That gets you into internal versus external iterators. That *is* an iterator, or the specification of one, but we are talking about mapping over trees. If you have a "map_tree" function, then iterators are unnecessary, besides being harder to implement [1]. To be fair, though, I've never used iterators per se, so that colors my experience. But I also think it's true that I've used trees more in the more powerful languages (chiefly Common Lisp and Perl) because trees are harder to work with in less-powerful languages (e.g. Pascal and Fortran), largely because of the lack of functional programming tools. > In other words, I believe the "collect values" cases and the > "nonlocal exit" cases are effectively disjoint. I've never found the > need for (e.g.) a "collect the first N nodes that satisfy X" routine. Returning paginated results in a web page. Personally, I have never encountered this sort of "partial results" problem where the data were stored in a tree. The database engine for a Web app may well have to deal with trees, if the underlying tables are tree- structured. But there the problem is "send to the client the first N rows that satisfy X" -- since N could be huge, I suspect that would have to be written in nonlocal-exit style, rather than by collecting the rows before sending them. -- Bob [1] Henry G. Baker, "Iterators: Signs of Weakness in Object-Oriented Languages", 1992. http://home.pipeline.com/~hbaker1/Iterator.html _______________________________________________ Boston-pm mailing list Boston-pm@mail.pm.org http://mail.pm.org/mailman/listinfo/boston-pm