Post History
Anonymous function A code golf answer can be an anonymous function[1]. That is, a function that can be defined without giving it a name (in programming languages that allow this). If an anonymous...
Answer
#3: Post edited
- ## Anonymous function
A code golf answer can be an anonymous function[^1]. That is, a function that can be defined without giving it a name (in programming languages that allow this).- If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score.
- ### Python example code: Fibonacci
- If a non-recursive approach is used, no function name is required:
- ```python
- lambda n:round(((1+5**.5)/2)**n/5**.5)
- ```
- If a recursive approach is used, the function must have a name to call:
- ```python
- f=lambda n:f(n-1)+f(n-2)if n>1else n
- ```
- In this particular case, the recursive approach is shorter, even with the additional 2 bytes for `f=`.
- ### Reasoning
- The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it.
- ```python
- outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10))
- for o in outputs:
- print(o)
- ```
[^1]: The additional rules in the [function answer](https://codegolf.codidact.com/posts/292347/292349#answer-292349) apply equally to anonymous functions.
- ## Anonymous function
- A code golf answer can be an anonymous function[^3]. That is, a function that can be defined without giving it a name (in programming languages that allow this).
- If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score.
- ### Python example code: Fibonacci
- If a non-recursive approach is used, no function name is required:
- ```python
- lambda n:round(((1+5**.5)/2)**n/5**.5)
- ```
- If a recursive approach is used, the function must have a name to call:
- ```python
- f=lambda n:f(n-1)+f(n-2)if n>1else n
- ```
- In this particular case, the recursive approach is shorter, even with the additional 2 bytes for `f=`.
- ### Reasoning
- The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it.
- ```python
- outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10))
- for o in outputs:
- print(o)
- ```
- [^3]: The additional rules in the [function answer](https://codegolf.codidact.com/posts/292347/292349#answer-292349) apply equally to anonymous functions.
#2: Post edited
- ## Anonymous function
A code golf answer can be an anonymous function. That is, a function that can be defined without giving it a name (in programming languages that allow this).- If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score.
- ### Python example code: Fibonacci
- If a non-recursive approach is used, no function name is required:
- ```python
- lambda n:round(((1+5**.5)/2)**n/5**.5)
- ```
- If a recursive approach is used, the function must have a name to call:
- ```python
- f=lambda n:f(n-1)+f(n-2)if n>1else n
- ```
- In this particular case, the recursive approach is shorter, even with the additional 2 bytes for `f=`.
- ### Reasoning
- The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it.
- ```python
- outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10))
- for o in outputs:
print(o)
- ## Anonymous function
- A code golf answer can be an anonymous function[^1]. That is, a function that can be defined without giving it a name (in programming languages that allow this).
- If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score.
- ### Python example code: Fibonacci
- If a non-recursive approach is used, no function name is required:
- ```python
- lambda n:round(((1+5**.5)/2)**n/5**.5)
- ```
- If a recursive approach is used, the function must have a name to call:
- ```python
- f=lambda n:f(n-1)+f(n-2)if n>1else n
- ```
- In this particular case, the recursive approach is shorter, even with the additional 2 bytes for `f=`.
- ### Reasoning
- The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it.
- ```python
- outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10))
- for o in outputs:
- print(o)
- ```
- [^1]: The additional rules in the [function answer](https://codegolf.codidact.com/posts/292347/292349#answer-292349) apply equally to anonymous functions.
#1: Initial revision
## Anonymous function A code golf answer can be an anonymous function. That is, a function that can be defined without giving it a name (in programming languages that allow this). If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score. ### Python example code: Fibonacci If a non-recursive approach is used, no function name is required: ```python lambda n:round(((1+5**.5)/2)**n/5**.5) ``` If a recursive approach is used, the function must have a name to call: ```python f=lambda n:f(n-1)+f(n-2)if n>1else n ``` In this particular case, the recursive approach is shorter, even with the additional 2 bytes for `f=`. ### Reasoning The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it. ```python outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10)) for o in outputs: print(o)