Hacker News new | past | comments | ask | show | jobs | submit login

I just don't understand where you would need an anonymous function with more then one line



You wouldn't want to have to declare named functions for the bodies of built-in control flow constructs like if/else statements and for loops, right? All of the reasons why also apply to why you might not want to declare named functions in order to pass them to other functions.

Read some Ruby code that uses blocks for an example of how passing anonymous functions can become a very clear and natural idiom. In Ruby, you can write your own simple control flow constructs in the form of functions that take a block -- in fact the idiomatic way to iterate through a collection in Ruby is with a function rather than a built-in control flow construct.

In languages like Lisp and Smalltalk, where functions can naturally take more than one in-line block, you can write any control flow construct yourself.


The usage case is the same as for a named function of more than one line.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: