What is map / reduce?

Asked

Viewed 572 times

11

Map / Reduce is a very common concept in JavaScript and many other languages. What it means and how it works in practice?

  • 1

    In Javascript (and many other languages) the function map serves to apply individually determined logic over all elements of an array, or similar. reduce, in turn, to reduce the array to a single value. However, I have no idea how this is applied in the Nosql database. If you can increment your question and put verifiable examples, it might be easier to view your question.

  • I edited removing the reference to Nosql database, because it does not apply to all cases, given the variety of implementation of this type of database.

  • In my humble and simplistic opinion, it is nothing more than, given a collection of information/values, to group its elements in a smaller set through a common identifier

3 answers

9


map(fn (elemento))

It is used to perform a transformation on all items of a array. Similar to the forEach(), with the difference that the callback of map() should always return a value - and this value will always be added to array as a result.

var arrOriginal = [1,2,3,4,5];

var arrFinal = arrOriginal.map(function (e) { return e * -1; });

console.log(arrFinal);

reduce(fn (valorCorrent, elemento), valorOriginal)

It is used to traverse a collection, accumulating and re-injecting the result for each interaction:

var vendedores = [
    { nome:'Adão', vendas:32 },
    { nome:'Bruno', vendas:23 },
    { nome:'Claudio', vendas:16 },
    { nome:'Dejair', vendas:7 },
    { nome:'Eduardo', vendas:4 },
    { nome:'Flavio', vendas:3 }
];

var totalVendas = vendedores.reduce(function(v, e) {
    return v + e.vendas;
}, 0);

console.log("Total de vendas: "+ totalVendas);

reduce() may also be used for structural transformations. The example below converts the list of objects to a single object with the name/sales properties pair of the converted elements to property/value:

var vendedores = [
    { nome:'Adão', vendas:32 },
    { nome:'Bruno', vendas:23 },
    { nome:'Claudio', vendas:16 },
    { nome:'Dejair', vendas:7 },
    { nome:'Eduardo', vendas:4 },
    { nome:'Flavio', vendas:3 }
];

var totalVendas = vendedores.reduce(function(v, e) {
    v[e.nome] = e.vendas;
    return v;
}, {});

console.log(totalVendas);

5

It has nothing to do with JS or Nosql (it was in the original question). Of course, they use the technique, but associating them with something fundamental doesn’t make sense. It was disseminated in functional languages where the declarative form (saying what to do and not how to do) imposes itself.

It’s usually a pair of functions, one that maps, that is, takes a collection of data and processes each item possibly filtering some information, calculating in some way or organizing in the expected way, and another that reduces the collection to a specific expected result based on the mapped items.

In general these functions are "how to do", and "what to do" is usually defined by Amble.

Of course nothing prevents using the same technique without specific functions.

It is a technique that ends up resembling SQL which is a declarative language.

In JS the function map() applies an anonymous function by receiving a parameter with each element that will be sent by the function. It is basically a for, which is even faster than the map(). It’s just a way of abstracting the loop. The function reduce() uses an anonymous function that receives two parameters, one is the accumulator (which will result in the reduction) and the other is the item to be accumulated (reduced).

Some languages have more specialized functions to help with performance. JS has some, such as filter().

As far as I know there is no technique that can optimize the use of these functions together, as occurs with LINQ, for example (which is not as good as it could be). So it’s just resource waste having to make at least 2 loops with the same data.

The advantage of technique is when there is a framework very efficient and able to produce better results than writing the bow in your hand, which is a very difficult thing to achieve. In the case of JS is only convenience (debatable). I wouldn’t call Mapreduce what JS does, given its simplicity.

2

Examples in C# with Linq:

Map, using the method Select:

var arrOriginal = new int[] { 1, 2, 3, 4, 5 };  
var arrFinal = arrOriginal.Select(n => n * -1);

Map Example in . Net Fiddle.

Reduce, using the Sum:

public class Vendedor
{
    public string Nome {get;set;}
    public int Vendas {get;set;}
}

var vendedores = new List<Vendedor>() { 
            new Vendedor { Nome = "Adão", Vendas = 32 },
            new Vendedor { Nome = "Bruno", Vendas = 23 },
            new Vendedor { Nome = "Claudio", Vendas = 16 },
            new Vendedor { Nome = "Dejair", Vendas = 7 },
            new Vendedor { Nome = "Eduardo", Vendas = 4 },
            new Vendedor { Nome = "Flavio", Vendas = 3 }
        };

var totalVendas = vendedores.Sum(v => v.Vendas);

Example of Reduce no . Net Fiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.