6

I’m doing my first internship and it's going good and I'm trying to keep up with everything the other programmers talk about. A senior programmer told me the other day that Ruby’s ability to pass code blocks directly into functions was much more preferable than using anonymous functions in JavaScript. It’s my understanding that JS anonymous functions are precisely the same thing – simply passing a code block via a variable as a parameter. Are there real-world examples of code block parameters being superior to anonymous functions? Or is it simply a matter of preference in one language over the other, a code readability/re-usability issue, or am I simply misunderstanding some other fundamental concept? This was a passing conversation about the relative merits of which language to use for some task, and not a concrete work problem, which is maybe why I’m confused. I Googled a couple naive examples of code blocks and had little trouble implementing them in JS. Looking forward to any input on the matter (anonymous or otherwise).

3
  • 1
    You're looking for something that's reusable. However you will use that piece of code is fully up to the programmer or the rule that are applied in a workgroup/development team. Inherently both are aiming for the same result. Lambda (anonymous) functions are just an option to use quick and short function-callbacks. In JavaScript it seems that they are often used from bigger blocks of code tho. As long as you know you would only use that little piece of function once in a call you probably are ok with lambda over giving the function it's own space (reusable). Commented Dec 22, 2013 at 20:13
  • 1
    As for Ruby, do you mean something like this: def meth1; yield if block_given?; end; meth1 { puts 42 }? Commented Dec 22, 2013 at 20:59
  • @DarekNędza Yes. Specifically that type of code block not lambda or some such. Commented Dec 22, 2013 at 21:06

1 Answer 1

5

They are not exactly the same. The main reason is the use of implicit and explicit return values.

You can read a good article here: http://yehudakatz.com/2012/01/10/javascript-needs-blocks/

Sign up to request clarification or add additional context in comments.

5 Comments

This is a truly fantastic read! "This impression, which I admittedly shared in my early days as a Ruby/JavaScript developer, misses an important subtlety that turns out to have large implications. This subtlety is often referred to as “Tennent’s Correspondence Principle”. In short, Tennent’s Correspondence Principle says: “For a given expression expr, lambda expr should be equivalent.” " Thanks for that!
It was in fact the exact answer. Accepted. I was busy reading the blog ;>
@wormtown You don't need anything more?
@DarekNędza No, thank you. phoet hit upon to exact answer I was looking for. The exact difference between the two.
Am I correct in my understanding that with the latest versions of ecma that this is no longer true?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.