The
map function is a useful abstraction in languages that poses it. The reason for it's usefulness, is that it provides the functionality for a rather common practice, which is to add something to every element in a set. This function is usually a method for the Array class, but it also works with Vectors and Matrices and other data structures. This is another example of
managed code. Instead of re-inventing what is already there, programmers should use the optimized functions that exist in a language so that the code would work in situations that are beyond the programmers control. Basic functions like
map will survive many versions of a programming language, but if you chose to code your own, there is no guarantee what libraries or techniques you might use and if they will be managed code. The
map function is a type of iterator, which helps keep programs short, simple, and readable.
Example (Ruby):
sampleArray = ["a", "b", "c", "d"]
sampleArray.map {|x| x + x.upcase}
Output => ["aA", "bB", "cC", "dD"]
No comments:
Post a Comment