erikrose/more-itertools — pull request #22 by abarnert (2012-11-29 11:11:59+00:00)
Title: Add collapse, interleave/interleave_longest, append/prepend.

Five new functions, all inspired by StackOverflow questions.

`collapse(iterable, basetype=None, levels=None)`

A multi-level `flatten`. Collapse a hierarchical iterator all the way down to strings and non-iterables (and instances of `basetype`, if given, which can be a type or a tuple), or down `levels` levels. People on SO keep asking "how do I flatten up to N levels instead of exactly N", or "how do I flatten iterators but not lists", or "why does my flatten screw up my strings". Rather than writing separate solutions for each, this is an all-singing-all-dancing-flatten-everything implementation.

`interleave(*iterables)` and `interleave_longest(*iterables)`

Like `flatten(zip(*iterables))` and `flatten(zip_longest(*iterables))`, except that `iterate_longest` skips missing entries instead of filling with None.

`append(iterable, value)` and `prepend(iterable, value)`

Yields from iterable, then yields value, or vice-versa. Only worth including because I tested every way I could think of to implement this, and using `chain(iterable, [value])` is by far the most efficient, which surprised someone on SO who kept arguing that it was a huge waste to build a length-1 list.

Unlike my previous pull requests, this time I found your unit tests and added tests for them. (But the "python3 setup.py test" doesn't work, because it tries to test the non-2to3 version.) I didn't go back and write tests for my other patches, because I'd have to cancel my pull requests, patch up each branch separately, submit new pull requests, and re-merge my fork's master, but if you want me to, I can.