title: sums of sections of array
body: <p>I have an array <code>xs</code> of numbers and I need to calculate sums of elements in three consecutive sections in it. the sections are <code>xs[0]</code>, <code>xs[1, N]</code> and <code>xs[N + 1, N]</code> for some integer <code>N</code>. For example, for <code>N = 2</code>:</p>
  
  <pre><code>[1, 2, 3, 4, 5] #=&gt; 1, 5, 9
  </code></pre>
  
  <p>my implementation is below. I could not make it more memory/speed efficient, but it still bothers me, I guess I have nothing else to do right now, but maybe there is a more elegant and succinct way to do this? I do not have to produce an array, it can be any other structure.</p>
  
  <pre><code>xs.each_with_index.reduce([0, 0, 0]) do |m, x|
    v, i = x
    j = (i &gt; N ? 2
         : i &gt; 0 ? 1
         : 0)
    m[j] += v.to_i
    m
  end
  </code></pre>