Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Say we have

var i = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

and want to reduce() it like

var plus = function(a, b)
{
  return a + b;
};

var s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  .reduce(plus);

console.log(s);

Now, I want to compose reduce() function itself from map() function.

How would you do that? What is the smartest way?

thanks!

share|improve this question
2  
OK great advice. –  Pointy Jan 7 at 2:35
3  
@KenOKABE I agree with Pointy, "I want to compose reduce function itself from map function" doesn't mean anything. –  plalx Jan 7 at 2:38
3  
@KenOKABE, you will have to explain your concept of composing a reduce function from map function. On the surface, it makes no sense. –  Roamer-1888 Jan 7 at 2:38
2  
Sorry, still a complete mystery. –  Roamer-1888 Jan 7 at 2:44
1  
@KenOKABE Can you clarify. Are you trying to build the map function using reduce (Which is possible)? Or are you trying to build the reduce function using map (which is not possible!). –  ravendano Jan 7 at 2:47

2 Answers 2

up vote 3 down vote accepted

If your trying to build the map function using reduce you could do the following (The example I'm providing will use built in functions and work for arrays only!):

var numbers = [1,2,3,4,5];
var map = function(arr, callback) {
  return arr.reduce(function(start, value) {
    start.push(callback(value));
    return start;
  }, []);
};

var newArray = map(numbers, function(value) {
  return value * 3;
});
console.log(newArray); // prints [3,6,9,12,15]

This will iterate through each of the values in our numbers array, invoke (execute) the callback function using the current value we're looping over and then push this value to an empty array which will be returned at the end of reduce. In other words it will map the results of our callback function to a new array!

That being said if your interested in functional programming I would encourage you checkout underscorejs's annotated source code.

If this is what your looking for please accept my answer!

share|improve this answer
    
Thank you very much. Is there any simple explanation why only reduce can compose map, not other direction? –  Ken OKABE Jan 7 at 5:30
1  
@KenOKABE Yes! Reduce can "fold" into any kind of data structure you want it to. It is primarily dependent on what you pass in to its initial start value and the value it returns from each callback. This is in contrast to map which only returns an array of values. –  ravendano Jan 7 at 5:40
    
understood. Thanks again. Can you please upvote this question and remove -1; it cannot be -1, and unpleasent to me. –  Ken OKABE Jan 7 at 20:57

map returns one value for every value in the array, thus the result will be an array just as big as the input array. The only way to get one value out of it is by sending only one value into it.

[[1,2,3,4,54]].map(
    function(d){
        sum = d.reduce(function(s,d){
                     return s+d
                 });
        console.log(sum)
    }
);
share|improve this answer
    
Thanks. I've got an error: app.js:6 console.log(s) ^ ReferenceError: s is not defined –  Ken OKABE Jan 7 at 21:00
    
Sorry about that, the return of the reduce function needs to be logged. –  surajck Jan 8 at 6:51
    
Thanks. I understand you idea. However, this is not reduce composition using map or other direction. –  Ken OKABE Jan 8 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.