J
Joshua Chia
Is there a way to run more than one closure per iteration through a
collection? For example, if I want to do the following two things, can
I combine them so that I only end up with one pass over the collection,
without doing a manual for loop?
o = a.select {|x| x >= 0}
p = a.inject(0) {|s, x| s += x}
Such a combination is useful when iterating through a collection is
expensive, e.g. on a large file on a slow disk.
collection? For example, if I want to do the following two things, can
I combine them so that I only end up with one pass over the collection,
without doing a manual for loop?
o = a.select {|x| x >= 0}
p = a.inject(0) {|s, x| s += x}
Such a combination is useful when iterating through a collection is
expensive, e.g. on a large file on a slow disk.