Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main difference between map and reduce

I used both methods but I am quite confused regarding the usage of both methods.

Is anything that map can do but reduce can not and vice versa?

Note: I know how to use both methods I am questioning for main difference between these method and when we need to used.

like image 543
Nishant Dixit Avatar asked Apr 20 '18 06:04

Nishant Dixit


People also ask

What is difference between map and reduce?

Generally "map" means converting a series of inputs to an equal length series of outputs while "reduce" means converting a series of inputs into a smaller number of outputs.

How do map and reduce functions differ in MapReduce?

In MapReduce, the map would emit a (word, count) pair for each word in each document. A MapReduce reduce() would then add up the count of each word in each document without mixing them into a single pile. So you get a list of words paired with their counts.

What is the difference between reduce and map in Python?

reduce() works differently than map() and filter() . It does not return a new list based on the function and iterable we've passed. Instead, it returns a single value. Also, in Python 3 reduce() isn't a built-in function anymore, and it can be found in the functools module.

Is reduce more efficient than map?

It's simple. . reduce() is absolutely the better choice because it only iterates through a list once while chaining multiple array iterative functions loops through an array n number of times where n is the number of chained iterative functions.


3 Answers

Source

Both map and reduce have as input the array and a function you define. They are in some way complementary: map cannot return one single element for an array of multiple elements, while reduce will always return the accumulator you eventually changed.

map

Using map you iterate the elements, and for each element you return an element you want.

For example, if you have an array of numbers and want to get their squares, you can do this:

// A function which calculates the square
const square = x => x * x

// Use `map` to get the square of each number
console.log([1, 2, 3, 4, 5].map(square))

reduce

Using an array as an input, you can get one single element (let's say an Object, or a Number, or another Array) based on the callback function (the first argument) which gets the accumulator and current_element parameters:

const numbers = [1, 2, 3, 4, 5]

// Calculate the sum
console.log(numbers.reduce(function (acc, current) {
  return acc + current
}, 0)) // < Start with 0

// Calculate the product
console.log(numbers.reduce(function (acc, current) {
  return acc * current
}, 1)) // < Start with 1

Which one should you choose when you can do the same thing with both? Try to imagine how the code looks. For the example provided, you can compute the squares array like you mentioned, using reduce:

// Using reduce
[1, 2, 3, 4, 5].reduce(function (acc, current) {
    acc.push(current*current);
    return acc;
 }, [])

 // Using map
 [1, 2, 3, 4, 5].map(x => x * x)

Now, looking at these, obviously the second implementation looks better and it's shorter. Usually you'd choose the cleaner solution, which in this case is map. Of course, you can do it with reduce, but in a nutshell, think which would be shorter and eventually that would be better.

like image 68
Ionică Bizău Avatar answered Oct 25 '22 12:10

Ionică Bizău


I think this picture will answer you about the difference between those Higher Order Functions

enter image description here

like image 69
Yazan Najjar Avatar answered Oct 25 '22 14:10

Yazan Najjar


Generally "map" means converting a series of inputs to an equal length series of outputs while "reduce" means converting a series of inputs into a smaller number of outputs.

What people mean by "map-reduce" is usually construed to mean "transform, possibly in parallel, combine serially".

When you "map", you're writing a function that transforms x with f(x) into some new value x1. When you "reduce" you're writing some function g(y) that takes array y and emits array y1. They work on different types of data and produce different results.

like image 29
tadman Avatar answered Oct 25 '22 14:10

tadman