Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

##Using uninitialized counters in recursion

Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

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

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer] [Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer] [Link to original answer]

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3^p3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3^p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

##Using uninitialized counters in recursion

Note: Strictly speaking, this is not specific to ES6. It makes more sense to use and abuse recursion in ES6, however, because of the concise nature of arrow functions.


It is rather common to come across a recursive function that's using a counter k initially set to zero and incremented at each iteration:

f = (..., k=0) => [do a recursive call with f(..., k+1)]

Under certain circumstances, it's possible to omit the initialization of such a counter and replace k+1 with -~k:

f = (..., k) => [do a recursive call with f(..., -~k)]

This trick typically saves 2 bytes.

###Why and when does it work?

The formula that makes it possible is ~undefined === -1. So, on the first iteration, -~k will be evaluated to 1. On the next iterations, -~k is essentially equivalent to -(-k-1) which equals k+1, at least for integers in the range [0 ... 231-1].

You must however make sure that having k = undefined on the first iteration will not disrupt the behavior of the function. You should especially keep in mind that most arithmetic operations involving undefined will result in NaN.

###Example #1

Given a positive integer n, this function looks for the smallest integer k that doesn't divide n:

f=(n,k=0)=>n%k?k:f(n,k+1) // 25 bytes

It can be shortened to:

f=(n,k)=>n%k?k:f(n,-~k) // 23 bytes

This works because n % undefined is NaN, which is falsy. That's the expected result on the first iteration.

[Link to original answer]

###Example #2

Given a positive integer n, this function looks for an integer p such that (3**p) - 1 == n:

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3) // 40 bytes

It can be shortened to:

f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) // 38 bytes

This works because p is not used at all on the first iteration (n<k being false).

[Link to original answer]

Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /