Skip to main content
Commonmark migration
Source Link

#Prefer .map over .reduce

Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of the .map versions, you will need to call t afterwards to get the actual value.

#Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of the .map versions, you will need to call t afterwards to get the actual value.

Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of the .map versions, you will need to call t afterwards to get the actual value.

deleted 16 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

#Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of these examples (except the ES6 one).map versions, you will need to call t afterwards to get the actual value.

#Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of these examples (except the ES6 one), you will need to call t afterwards to get the actual value.

#Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of the .map versions, you will need to call t afterwards to get the actual value.

Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

#Prefer .map over .reduce

Consider the following code for summing an array:

a.reduce(function(x,y){return x+y})

Pretty long, right? What if I told you that you could get rid of the return? Well, you can:

a.map(function(x){t+=x},t=0)    // 7 bytes saved

(Although an even shorter way is eval(a.join("+")).)

How about reducing by multiplication, where you have to specify the starting number anyway?

a.reduce(function(x,y){return x*y},1)  // Looooong
a.map(function(x){t*=x},t=1)    // An easy 9 bytes shorter

(Again, eval(a.join("*")) works as well.)

Here, let's try one that doesn't work with eval(a.join()):

a.reduce(function(x,y){return x+f(y)})
a.map(function(x){t+=f(x)},t=0)

Note that this doesn't work quite as well with ES6, although it's still a little shorter:

a.reduce((x,y)=>x+f(y))
a.map(x=>t+=f(x),t=0)

Note: in all of these examples (except the ES6 one), you will need to call t afterwards to get the actual value.