Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Meta

Post History

50%
+0 −0
Meta Default Rules: Code Golf Answer Types

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...

posted 3mo ago by trichoplax‭  ·  edited 3mo ago by trichoplax‭

Answer
#3: Post edited by user avatar trichoplax‭ · 2024-08-26T22:20:44Z (3 months ago)
Change footnote to 3 to avoid clash with 1 and 2 in another answer on this page
  • ## 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 by user avatar trichoplax‭ · 2024-08-26T22:19:00Z (3 months ago)
Link to main function answer
  • ## 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 by user avatar trichoplax‭ · 2024-08-26T21:43:00Z (3 months ago)
## 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)