Skip to main content
Commonmark migration
Source Link

##Using the currying syntax for dyadic and recursive functions

Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Dyadic functions

Whenever a function takes exactly two arguments with no default values, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments with no default values, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

Using the currying syntax for dyadic and recursive functions

Dyadic functions

Whenever a function takes exactly two arguments with no default values, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments with no default values, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments with no default values, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

##Using the currying syntax for dyadic and recursive functions

###Dyadic functions

Whenever a function takes exactly two arguments, using the currying syntax saves one byte.

Before

f =
(a,b)=>a+b  // 10 bytes

Called with f(a,b)

After

f =
a=>b=>a+b   // 9 bytes

Called with f(a)(b)

Note: This post in Meta confirms the validity of this syntax.

###Recursive functions

Using the currying syntax may also save some bytes when a recursive function takes several arguments but only needs to update some of them between each iteration.

Example

The following function computes the sum of all integers in the range [a,b]:

f=(a,b)=>a>b?0:b+f(a,b-1)   // 25 bytes

Because a remains unchanged during the whole process, we can save 3 bytes by using:

f =                         // no need to include this assignment in the answer anymore
a=>F=b=>a>b?0:b+F(b-1)      // 22 bytes

Note: As noticed by Neil in the comments, the fact that an argument is not explicitly passed to the recursive function does not mean that it should be considered immutable. If needed, we could modify a within the function code with a++, a-- or whatever similar syntax.

Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading