title: Cyclical Sliding Window Iteration
body: <p>Consider some given sequence and a window length, say the <code>list</code></p>
  
  <pre><code>a = [13 * i + 1 for i in range(24)]
  </code></pre>
  
  <p>(so that </p>
  
  <pre><code>In [61]: a
  Out[61]: 
  [1,
   14,
   27,
   40,
   ...,
   287,
   300]
  </code></pre>
  
  <p>)</p>
  
  <p>and window length 3.</p>
  
  <p>I'd like to take the sliding window sum of this sequence, but cyclically; i.e., to calculate the length-24 <code>list</code>:</p>
  
  <pre><code>[sum([1, 14, 27]),
   sum([14, 27, 40]),
   ...,
   sum([287, 300, 1]),
   sum([300, 1, 14])]
  </code></pre>
  
  <p>The best I could come up with, using <a href="https://docs.python.org/2/library/collections.html#collections.deque"><code>collections.deque</code></a> and <a href="http://p-nand-q.com/python/lambda.html">Stupid Lambda Tricks</a>, was</p>
  
  <pre><code>d = collections.deque(range(24))
  d.rotate(1)
  map(lambda _: d.rotate(-1) or sum(a[i] for i in list(d)[: 3]), range(24))
  </code></pre>
  
  <p>Is there something less horrible?</p>