Latest recipes tagged "iterable"http://code.activestate.com/recipes/tags/iterable/new/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 &lt;&lt; i &lt;&lt; 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> Truth Value Aware Iterable (Python) 2013-06-11T08:00:25-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578549-truth-value-aware-iterable/ <p style="color: grey"> Python recipe 578549 by <a href="/recipes/users/4169882/">Alan Franzoni</a> (<a href="/recipes/tags/boolean/">boolean</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/truth/">truth</a>). </p> <p>This small recipe enables truth value testing on iterables.</p> <p>It is quite common to do things like:</p> <pre class="prettyprint"><code>if somesequence: ... else: ... </code></pre> <p>Such constructs, that enter the if block if the sequence's got one or more elements and the else block if it's empty, work fine on non-lazy builtin sequences (lists, strings, tuples) and dictionaries as well, but doesn't necessarily work on generic iterables - most of them are always true regardless of their contents, since they're some kind of object. A classical example is generators, but such behaviour can be extended to any object implementing the Iterable interface.</p> <p>Just wrap your iterable with this decorator and you'll get a truth-aware iterable which supports proper truth testing by doing a small first element prefetching and can then be used just like the original 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>