views:

127

answers:

2

Perl's Data::Rmap allows you to recursively evaluate a BLOCK over a list of data structures (locally setting $_ to each element) and return the list composed of the results of such evaluations. $_ can be used to modify the elements.

This is useful for iterating over things like nested hashes, or hierarchies of arrays of hashes and the like.

+1  A: 

Without really looking into details, I'm not sure you need a module for that in Ruby. Iterators and blocks are there to do what you want.

Keltia
Edit to post an example or a link to an example by chance?
dreftymac
Check Gaius' answer, it has more details :)
Keltia
+2  A: 

Ruby's Enumerable does everything you want, I think. "... and return the list composed of the results of such evaluations" indicates you want Enumerable#map. My first go would be something like this:

[ {...}, {...}, {...}, ... ].map do |hash|
  hash.something
  do_other_stuff_with(hash)
  hash                  # important to have as last line b/c of how #map works
end
James A. Rosen
AFAICT, this is not equivalent, because Data::Rmap works with more than just ArrayOfHash (AoH). It can also iterate more complex structures such as ArrayofArray (AoA), HashofArray (HoA), and others (AoAoH), (HoHoA) and so forth ad-infinitum.
dreftymac
.map will happily work on Arrays of things other than Hash. What it won't do is recursion. Perhaps a Y-combinator is in order?
James A. Rosen