## Csci 4651 Problem set 7

### Problem 1 (and only): 25 points

Your task is to implement the following public methods of the NestedArray class. Consult the description of Enumerable module for methods' behavior. The starting point of the code is here.

1. write `to_s` method to return the contents of the nested array as a string. Make sure that all the nested elements appear in the string and the structure of the nesting is displayed.
2. write `find` method that returns any one element (in any of the nested arrays) for which the given block is not false. For instance, if I consider the nested array ```NestedArray.new([[6,7],[8],[[9, 6]]]).find {|y| y > 7}``` then the method may return 8. Note that returning 9 would be correct as well. If the block is false on all elements, nil is returned.
3. write `any?` method that returns true if the block returns a value other than false or nil on at least one element, otherwise it returns false
4. write `map` method that returns a new nested array of the same structure with the results of running block once for every element. For instance,
``````
NestedArray.new([[6,7],[8],[[9, 6]]]).map {|y| y * y}
``````
returns a NestedArray object `[[36,49],[64],[[81, 36]]]`.
5. write `inject` method that takes a "seed" and works on all elements according to the definition in the Enumerable module. For instance,
``````
NestedArray.new([[6,7],[8],[[9, 6]]]).inject(1) {|prod, y| prod * y}
``````
returns the product of all elements: 18144.

Don't worry about default parameters or type mismatches for any of the methods: they need to work on the correct input and are allowed to break on any incorrect data.

You may define any private methods that you find helpful. If you run into any problems with making methods private, you can make your helper methods public without loss of credit, just use _ in front of the name to distinguish "private" methods from public ones.

There are two ways to approach this problem: use the `each` method above as a sample and just write all the other methods one by one, or write a more general traverse-like method and use it to generate all of the needed methods. The latter approach requires using a lambda rather than a block since we need to pass two functions to traverse (`action` and `combine`), but we can pass only one block.

You may use either approach. No extra credit is given for the lambda approach because once you get `traverse` to work, the rest of the methods can be written in 20 minutes, if not less, so you are simplifying the work quite a bit.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.