The group_by method from Ruby’s Enumerable mixin (and compared with Scala)

A few weeks ago I wrote about the groupBy method from Scala’s collection library. Today I will port those example to Ruby for which there is also such a method, just with s slightly different name (group_by instead of groupBy).

For the idea behind the method, see the post about Scala’s version. Here I will put the Scala code from the other post and show the equivalent code for Ruby next to it.
Note: I’ve used Ruby 1.9.2p290 for all examples.

1st Example:

Here we group an Array (in Scala List) of strings (with names of different bird species) by their first name.

Scala:

Ruby:

The value of the resulting Hash is shown in the comment. The line breaks were added by me to make it more readable. They are not part of the result.

2nd Example:

Here we group and Array (in Scala List) of strings by their length.

Scala:

Ruby:

3rd Example:

Here we group some raptor (birds of prey) species by their kind. Everything that is not an Eagle or Falcon will be “Unknown”.

Scala:

Ruby:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
raptors = [“Golden Eagle”, “Bald Eagle”, “Prairie Falcon”, “Peregrine Falcon”,
           “Harpy Eagle”, “Red Kite”]
 
kinds = raptors.group_by do |species|
case species
    when /.*Eagle.*/
    Eagle
  when /.*Falcon.*/
    Falcon
  else
    unknown
  end
end
 
# {Eagle=>[Golden Eagle, Bald Eagle, Harpy Eagle],
# Falcon=>[Prairie Falcon, Peregrine Falcon], unknown=>[Red Kite]}

4th Example

Here we take an Array (List in Scala) of String and group them by the number of occurrences in the Array.

Scala:

Ruby:

Ruby’s Hash class does not have a mapValues method like Scala’s Maps do (at least I couldn’t find one in the standard library) , so I used inject instead which is very similar to foldLeft in Scala. This is not as elegant as mapValues but get’s the job done as well.

Conclusion

As you can see the Ruby version of the group_by method is as easy to use as Scala’s version. Both Ruby and Scala provide a lot of great tools to work with collections. Scala’s collection library is much bigger than Ruby’s standard collections, though. Scala supports many more data structures and also a lot of immutable data structures because Scala also encourages functional programming (whereas Ruby is mostly only OOP).

No matter what language you use, learn as much about the available collections in the standard library (or available open source packages) as possible because this will save you a lot of time when using those languages.

Clubs that are very, very, very difficult for the average player to hit, they’ve come out with a hybrid which has more the same loft, 21, 24, 23, 19, the same kind of loft as the iron, but a much different http://www.wholesale-cheap-nfl-jerseys.com shape to the head with a more wide rounded sole to it, makes it much much easier to hit, but as soon as people put those in their hands, their tendency is to line the ball up toward the front foot a lot like they do a fairway wood, and then they have difficulty with that. That allows you to effectively use all the hand positions with pretty minimal change in your overall neck or back position. So, here I am in the drop you can see my arm still has a nice bend in it. I can get my back considerably flatter, and as I cycle through the pedals I’m not hitting my knees wholesale nfl jerseys on my chest. A Example serial sections of anterior, middle and posterior portions of dLGN in a mouse with GFP expressing On Off DSGC axons that was injected with in superficial layers of V1. a, DAPI to show cytoarchitectural landmarks and dLGN borders. b, GFP+ DSGC axons and AAV2 Glyco hGFP infected cell bodies (see main Fig. 4 and text). c, Mask of GFP+ DSGC axons (Methods). d, dLGN relay neurons. e, GFP+ DSGC Cheap NFL Jerseys axon mask superimposed with mCherry signal; this was used to determine colocalization. f, mCherry and GFP signals merged. Framed region in a is shown at higher magnification in b Arrowhead (a), thalamocortical axon of mCherry+ dLGN cell. f Arrowhead, site of GFP/mCherry co localization that does not Fake ray bans contain VGLUT2; arrow, GFP/mCherry/VGLUT2+ contact.He wasn’t just being paranoid. Land confiscation is one of the leading causes of pissed off people in China right now. Losing your home to new roads, government buildings and even luxury villas kind of has that effect. Knowing some hired goons would show up sooner or later to force him off his land, Yang got to work.Modern cheap jerseys sports stadiums basically double as shopping malls or luxury resorts. They are marvels of modern design that sometimes make the game being played within their confines seem like the least interesting thing you could be doing while you’re there. Designing a stadium is an art, though, and the simplest mistakes can lead to absolute chaos for teams on the field. Mistakes like .My undergraduate degree (from the College of New Jersey) is actually based in physics, but I took advantage of several different opportunities during college in order to maximize my experiences with astronomy. I took the few astronomy courses my department offered, worked at the campus observatory, and did a semester long independent research project with one of my professors in the spring of 2006 (which involved studying the properties of an eclipsing binary star system). Then, I spent the summer of 2006 participating in a Research Experience for Undergraduates (REU) program sponsored by the National Science Foundation. The program was held at Arecibo Observatory in Puerto Rico, and my project involved studying a cheap football jerseys massive star forming region using data taken with the Arecibo radio telescope. That program was truly a life changing experience for me, and I’ll never forget how much fun I had while I was there! Between these experiences, I was sold on astronomy and proceeded to apply to astronomy cheap jordans programs for graduate school.