Created
July 6, 2017 18:05
-
-
Save pashagray/7f7d9048cb38acecb8d82b177b132380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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