Skip to main content
Commonmark migration
Source Link

#Yet another way to avoid return

Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a returnuse eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

deleted 13 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

Example

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

Example

#Yet another way to avoid return

You know you should use eval for arrow functions with multiple statements and a return. In some unusual case you can save more using an inner subfunction.

I say unusual because

  1. The result returned must not be the last expression evalued in the loop

  2. There must be (at least) 2 different initializations before the loop

In this case you can use an inner subfunction without return, having one of the initial values passed as a parameter.

Example Find the reciprocal of the sum of exp function for values in a range from a to b.

The long way - 55 bytes

(a,b)=>{for(r=0,i=a;i<=b;i++)r+=Math.exp(i);return 1/r}

With eval - 54 bytes

(a,b)=>eval("for(r=0,i=a;i<=b;i++)r+=Math.exp(i);1/r")

With an inner function - 53 bytes

(a,b)=>(i=>{for(r=0;i<=b;i++)r+=Math.exp(i)})(a)||1/r

Note that without the requirement of a lower range limit a, I can merge the initializations of i and r and the eval version is shorter.

added 1 character in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading