6 ms·
For me, it's first class function support. Of course, there are other "real functional languages" like Scala, Haskel or whatever, but they very uncommon. JS is
by tomca32 10y ago
For me, it's first class function support. Of course, there are other "real functional languages" like Scala, Haskel or whatever, but they very uncommon.
JS is the only commonly (Clojure is very uncommon) used language with real first class functions and not some silly hack like Ruby/Python lamdas, procs, or blocks.
To be fair, it seems that Java 8 has first class functions. I can't say much about it since I didn't get a chance to play with it yet.
- collyw 10y agoHow are they a hack in Python?
- randomguy1254 10y agolambda bodies can only be a single expression
- collyw 10y agoThere is more to functional programing in python than lambda functions. Normal functions can be created and passed around.
- randomguy1254 10y agoSure, but I think that is the point. You can do that, but its not as convenient and becomes harder to read. And besides, those are free functions you speak of, not closures. To emulate multi-line closures becomes even more obtuse.
- collyw 10y agoI find it more readable than lambda function syntax personally. And still way nicer than messing around in JavaScript.
- randomguy1254 10y agoSorry, I edited my comment and you may have missed it. How would you emulate multi-line closures in Python? A free function which you can pass to a higher order function is not a closure. I think you would need to create a functor, and that becomes even more verbose.
- Chris2048 10y agoWhat do you mean by closure in this case?
- randomguy1254 10y agoClosures are functions that can capture values/references visible in the scope where the function is defined/instantiated. They are basically a short hand way of creating function objects, functions which have data members.
- Chris2048 10y agoI believe all functions are object in python, so you can add attributes to them, but you'd be better of creating a class with a __call__ method. Relevant scope can be captured in the class __init__. Classes themselves can hold multiple functions/methods, and if you want a function that carries state, maybe a generator would be useful?
- randomguy1254 10y agoI think this is the point. You can do all these things, but they are all extra hoops you have to jump through when all you want/need is an anonymous on-the-spot closure.
- Chris2048 10y agoI'll grant you that a class isn't an anonymous function, arguably it's the same with lambdas that aren't as powerful as proper function defs. But I'm not sure this is a problem. In fact, in some ways it may be advantageous e.g. static analysis. I'd need a use-case for the anonymous closure, but a generator is a basic concept in python, so I son't think it's any trickier than a js function. I know how in js there is a degree of 'binding' scope and hiding functionality in closures - I think in python (for better or worse) things would by convention be implemented differently. As an aside, would you say js scope/closure approach is a bit similar to the approach in R?
- edem 10y agoJava is a different best. It is used for a lot of enterprise projects but you don't want to use it on your own hobby projects. You can't get things done in time.
- sjtgraham 10y agoThis is the lambda syntax in Ruby: Defining a lamba `foo = ->(x) { x * x }` Calling it `foo[10]` or `foo.call(10)` I don't see anything hacky about that at all.