Skip to content

Instantly share code, notes, and snippets.

@kerryb
Created March 9, 2012 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerryb/2007129 to your computer and use it in GitHub Desktop.
Save kerryb/2007129 to your computer and use it in GitHub Desktop.
Reduce list to average of chunks of n items
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
chunk_size = 3
chunked_averages = (0...data.length).zip(data).group_by{|a| a.first / chunk_size}.values.map{|a| a.map(&:last).reduce(&:+) / a.size}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment