Is there a difference between foreach and map?

Robert Gould picture Robert Gould · Dec 10, 2008 · Viewed 67.2k times · Source

Ok this is more of a computer science question, than a question based on a particular language, but is there a difference between a map operation and a foreach operation? Or are they simply different names for the same thing?

Answer

madlep picture madlep · Dec 10, 2008

Different.

foreach iterates over a list and applies some operation with side effects to each list member (such as saving each one to the database for example)

map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list of strings to uppercase)