Popular recipes tagged "iterable" but not "boolean"http://code.activestate.com/recipes/tags/iterable-boolean/2014-10-02T12:13:52-07:00ActiveState Code RecipesNumbers as Ranges - iterable integers (C++)
2014-10-02T12:13:52-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578945-numbers-as-ranges-iterable-integers/
<p style="color: grey">
C++
recipe 578945
by <a href="/recipes/users/4187847/">elazar</a>
(<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/range/">range</a>).
Revision 2.
</p>
<p>This header file makes simple integers iterable.
Note that it works only on C++11 or above.</p>
<p>Usage:</p>
<pre class="prettyprint"><code>#include "num_range.h"
for (int i : 3) cout << i << endl;
</code></pre>
<p>Output:
0
1
2</p>
<p>Implementation note:
This code is far too generic. We only need <code>DerefableInt</code>.
The templates are there for no practical reason.</p>
<p>Cons:
pollutes namespace std;
nonstandard idiom;</p>
Cycling a sequence (Python)
2014-09-20T19:20:46-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/578942-cycling-a-sequence/
<p style="color: grey">
Python
recipe 578942
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>A post-it function to cycle through some sequence. Better use itertools.cycle if for any iterable.</p>
Wrap any iterable context manager so it closes when consumed (Python)
2012-11-19T20:10:35-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/578342-wrap-any-iterable-context-manager-so-it-closes-whe/
<p style="color: grey">
Python
recipe 578342
by <a href="/recipes/users/4184316/">Andrew Barnert</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
</p>
<p>There are a few types in Python—most notably, files—that are both iterators and context managers. For trivial cases, these features are easy to use together, but as soon as you need to use the iterator lazily or asynchronously, a with statement won't help. That's where this recipe comes in handy:</p>
<pre class="prettyprint"><code>send_async(with_iter(open(path, 'r')))
</code></pre>
<p>This also allows you to "forward" closing for a wrapped iterator, so closing the outer iterator also closes the inner one:</p>
<pre class="prettyprint"><code>sync_async(line.upper() for line in with_iter(open(path, 'r')))
</code></pre>
Iterator Offsetter (Python)
2012-04-16T13:08:55-07:00Josh Bodehttp://code.activestate.com/recipes/users/4179046/http://code.activestate.com/recipes/577852-iterator-offsetter/
<p style="color: grey">
Python
recipe 577852
by <a href="/recipes/users/4179046/">Josh Bode</a>
(<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/offset/">offset</a>).
Revision 2.
</p>
<p>Produces a list of copies of an iterable that are offset by the supplied offsets.</p>
Windowing an iterable with itertools (Python)
2010-04-15T18:45:41-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577196-windowing-an-iterable-with-itertools/
<p style="color: grey">
Python
recipe 577196
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/peeking/">peeking</a>, <a href="/recipes/tags/window/">window</a>).
</p>
<p>Oftentimes a programmer needs to peek into an iterator without advancing it, a task for which many good solutions already exist. But what if the intrepid coder needs a fast and pythonic way to 'window' the data? This recipe demonstrates how to wrap any iterable with a class that adds two methods, prev and peek.</p>