Skip to content

Instantly share code, notes, and snippets.

@pashagray
Created July 6, 2017 18:05
Show Gist options
  • Save pashagray/7f7d9048cb38acecb8d82b177b132380 to your computer and use it in GitHub Desktop.
Save pashagray/7f7d9048cb38acecb8d82b177b132380 to your computer and use it in GitHub Desktop.
def apply_on_set(arr, set_size, &block)
arr.each_cons(set_size).map { |set| yield(set) }
end
apply_on_set([1, 2, 3, 4], 2) { |set| set.reduce(:+) } #=> [3, 4, 7]
apply_on_set([1, 2, 3, 4], 3) { |set| set.reduce(:+) } #=> [6, 9]
apply_on_set([1, 2, 3, 4], 2) { |set| set.map(&:to_f) } #=> [[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment