Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Map Function  RSS feed

 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
can anyone tell me when we should use map function?

Regards,
Gunjan
 
Sean Corfield
Ranch Hand
Posts: 314
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general 'map' is appropriate when you want to take a collection of items and produce a new collection with each item transformed in a particular way. The input and output collections will have the same length and each successive element of the output collection corresponds to the successive elements of the input collection.

If you want to produce a result that has a different shape to the input collection, you would use 'reduce' or 'fold' (different languages call them different things).

Does that help?
 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Corfield wrote:In general 'map' is appropriate when you want to take a collection of items and produce a new collection with each item transformed in a particular way. The input and output collections will have the same length and each successive element of the output collection corresponds to the successive elements of the input collection.

If you want to produce a result that has a different shape to the input collection, you would use 'reduce' or 'fold' (different languages call them different things).

Does that help?


Thanks for reply Sean. Actually I have a code. I am not able to get why we are using map function in this code?

 
Tim Cooke
Marshal
Posts: 3838
221
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're getting a little mixed up on this one.

What you have here is a Map data structure, sometimes called a Dictionary in other languages, rather than a map function as found in the functional programming paradigm.

An example of a map function might be to find the squares of all the numbers in a list. Take particular notice of line 4.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!