Compound Data Structures? Break It Down.

I had an interesting conversation with one of my classmates yesterday.  She said that she had struggled with last week's lesson in compound data structures using Ruby, so I asked if she would like some help.

Rather than tackle compound data structures as a whole, I tried to break the problem down into its components.  I figured that the three main pieces of utilizing compound data structures (at least in the way that we were taught at Launch Academy) were arrays, hashes, and each loops.

We talked about arrays for a while, and I came up with what will now be my go-to example: a swim meet.  Let's say our friend Billy is competing in a race.  He's in lane 3, and we want to cheer him on. 


The swimming pool can be represented by an array with each item being an individual swimmer.

swimming_pool = ["Sam", "Jane", "Billy", "John"]

We have to use the index of his lane in order to make sure that Billy hears our message.  


puts "Go Billy!" can be represented as puts "Go #{swimming_pool[2]}!"


Then, we decide to cheer on all of the swimmers; it's only fair, after all. Using an each loop lets us turn that single cheer into a cheer for each swimmer in turn.  Because we were using Ruby, we also got to talk about the pipes used around our variable in the each loop, which even look like the lanes of a swimming pool!

swimming_pool.each do |swimmer|
  puts "Go #{swimmer}!"
end


Because the initial confusion was around working with compound data structures, I wanted to use this same example with nested arrays.  I figured that, if she felt comfortable with arrays and nested arrays, substituting a hash for one of those arrays wouldn't be too big of a leap.  

Going back to our swimming pool, now each lane has two swimmers in it!

swimming_pool = [["Sam", "Jane"], ["Billy", "John"]]

We still want to greet all of them equally, so we use an each loop.  The benefit of an example like this is that we are putting our each loop from before (greeting each swimmer) inside another each loop (going to each lane and greeting each swimmer in the lane.)

swimming_pool.each do |lane|
  lane.each do |swimmer|
    puts "Go #{swimmer}!"
  end
end


Since this is an example that looks intimidating to someone who was worried about manipulating compound data structures, now they can see where each piece comes from.  Stepping back from the example, we have now also modeled a good way to break down a challenge into pieces. 

1. What code would you use for a single example?
2. What code would you use to access that single example?
3. How would you put that in a loop to access all examples?

We also have a nice transition into hashes, because we can easily turn our swimming pool into a hash:

swimming_pool_hash = {
  "lane_one" => ["Sam", "Jane"],
  "lane_two" => ["Billy", "John"]
}


We can take the same steps as before:

1. What code would you use for a single example?
puts "Go Billy!"

2. What code would you use to access that single example?
puts "Go #{swimming_pool_hash["lane_two"][0]}!"

And for a single lane:

lane.each do |swimmer|
  puts "Go #{swimmer}!"
end


3. How would you put that in a loop to access all examples?

swimming_pool_hash.each do |key, value|
  value.each do |swimmer|
    puts "Go #{swimmer}!"
  end
end


Now you've broken down compound data structures and the most common components, as well as given an example of how to beak down problems into pieces.

You've also given a miniature example of good sportsmanship, too.  Go Billy!

Comments