On Aug 2, 2019, at 11:34, Christopher Barker <pythonchb@gmail.com> wrote:
The other issue here, in the context of the OP's proposal, is that it's less than obvious that that's what a user would want when they want to operate on all the items in a iterable without creating a new iterable. To Guido's point, the way to do this now is:
for i in an_iterable: act_on(i)
Is simple, obvious and compact, though not "functional" or "using the comprehension syntax".
Where I find consume useful is when I already have an iterator. I have a long chain of functional-style iterable transforms (mostly genexprs and itertools calls) leaving me with a final thing that represents the whole computation, but it’s lazy and I need it strictly evaluated now (generally because it has side effects, or because I don’t know from inspection whether it does). If I don’t already have an iterator, I wouldn’t create one just to pass to consume to run some side effects; I’d write your example the same way Guido suggested. The OP’s only problem is that he created an unnecessary genexpr, and the solution is to just not create the unnecessary genexpr. It’s one of those “Doctor, Doctor, it hurts when I punch myself in the neck” “OK, so don’t punch yourself in the neck” cases. But… when someone really does seem to be looking for consume, even in a place where I wouldn’t use it. I may tell them why I wouldn’t use it, but I’ll still tell them that the function they’re looking for is named consume, and where to find and/or how to write it. In fact, looking back at the message that started this whole side track, that’s exactly what I did. If it had already been in itertools, I’d still have explained why I think his #3 is better than his #2, but the rest would just be one line: “But if you really want that run function, it already exists: `from itertools import consume as run`”. I don’t think that’s a big enough win on its own to be worth someone rewriting itertools so consume can be added. But maybe consume plus grouper plus some of the less trivial recipes (or, more simply, just all of them, to avoid bikeshedding arguments on each one) is.