Difference between map and collect in Ruby?

There’s no difference, in fact map is implemented in C as rb_ary_collect and enum_collect (eg. there is a difference between map on an array and on any other enum, but no difference between map and collect). Why do both map and collect exist in Ruby? The map function has many naming conventions in different languages. Wikipedia provides an overview: The map function originated in functional programming languages but is today supported (or may be … Read more

“Cannot create generic array of ..” – how to create an Array of Map?

Because of how generics in Java work, you cannot directly create an array of a generic type (such as Map<String, Object>[]). Instead, you create an array of the raw type (Map[]) and cast it to Map<String, Object>[]. This will cause an unavoidable (but suppressible) compiler warning. This should work for what you need: You may … Read more

Determine if map contains a value for a key?

Does something along these lines exist? No. With the stl map class, you use ::find() to search the map, and compare the returned iterator to std::map::end() so Obviously you can write your own getValue() routine if you want (also in C++, there is no reason to use out), but I would suspect that once you get the hang of using std::map::find() you won’t … Read more

What does the “map” method do in Ruby?

The map method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use map!): Array and Range are enumerable types. map with a block returns an Array. map! mutates the original array. Where is this helpful, and what is the difference between map! and each? Here is an example: … Read more

Simple dictionary in C++

If you are into optimization, and assuming the input is always one of the four characters, the function below might be worth a try as a replacement for the map: It works based on the fact that you are dealing with two symmetric pairs. The conditional works to tell apart the A/T pair from the … Read more