views:

150

answers:

2

Im new to "real" Javascript:ing and I know understand more of functional programming.

It seems that in Javascript you get the best from both worlds: functional and object oriented programming.

But in Ruby, you don't have first class functions (function as a datatype).

Does this mean that Javascript embraces the best of the both worlds while Ruby only embraces object-oriented programming style?

A: 

There is a difference between coding in a functional manner and coding in a functional language. Ruby may not have many of the amenities of a functional language, but if you learn to think functionally you'll find ways to use Ruby to that end.

What really matters is the paradigm--the thought process you use to construct solutions. Funtional languages can be used to write procedural code (just like OO languages), but if you understand the different paradigms then you can apply the approach best suited for your scenario.

If you're into books, I enjoyed "Real World Functional Programming" (with Mr. Skeet as co-author)

STW
@STW: isn't there a good book about Functional Programming but using Javascript?
never_had_a_name
none specifically that I can recommend
STW
what do you want to do? The functional approach lends itself to a specific type of problem, and outside of math and science you'll usually need a second language to achieve your goal (or for a language which supports putting a boundary between your stateless/pure functional code and your stateful/real-world code)
STW
Well, yes and no. Yes, it's the paradigm that matters. But by 'functional programming' it is usually understood to mean the 'lisp' paradigm. How can you do FP if you don't have first class functions? (or alternatively, first class code blocks or lambdas which are not-quite-functions or even function pointers/reference). Ruby does have lambdas by the way, so you can do FP in Ruby. But it would be hard to do FP in a language that can't pass bits of code as argument.
slebetman
+7  A: 

Ruby does have first class functions. What makes you think it doesn't? From wikipedia: A language that has first-class functions is one where:

The language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions.

You're probably hung up on the fact that Ruby's Procs/Lambdas need to be invoked via a call() but this does not impact on their firstclassness. Common Lisp lambdas are invoked with a funcall as well and I do not recall anyone claiming Common Lisp does not have first class functions.

banister
sepp2k: see here: http://cl-cookbook.sourceforge.net/functions.html it appears that in common lisp (unlike scheme) you're required to invoke a lambda (bound to a variable) by using `funcall`: e.g (let ((foo (lambda (x) x))) (funcall foo y))
banister
@banister: You're right, of course. I was momentarily confused.
sepp2k