When I use jquery library I notice that the code will continue execution regardless the previous line complete the execution. More precisely while using jquery ajax, it will continue execution without waiting for the ajax result. Is this because javascript takes advantage of parallel processing? Or to which category this can be referred to. Also can we call javascript as a functional programming language.?
It will be greatly appreciated if someone could explain this briefly. Thanks in advance.
Javascript, functional programming and parallel processing
functional programmingjavascript
Related Solutions
In which scenarios should I consider a functional programming languages better suited to do a given task? Besides the so recently popular multicore problem of parallel programming.
Anything that involves creating sequence of derived data elements using a number of transformation steps.
Essentially, the "spreadsheet problem". You have some initial data and set of row-by-row calculations to apply to that data.
Our production applications do a number of statistical summaries of data; this is all best approached functionally.
One common thing we do is a match-merge between three monstrous data sets. Similar to a SQL join, but not as generalized. This is followed by a number of calculations of derived data. This is all just functional transformations.
The application is written in Python, but is written in a functional style using generator functions and immutable named tuples. It's a composition of lower-level functions.
Here's a concrete example of a functional composition.
for line in ( l.split(":") for l in ( l.strip() for l in someFile ) ):
print line[0], line[3]
This is one way that functional programming influences languages like Python.
Sometimes this kind of thing gets written as:
cleaned = ( l.strip() for l in someFile )
split = ( l.split(":") for l in cleaned )
for line in split:
print line[0], line[3]
If I decided to switch to a functional programming language which do you consider are the biggest pitfalls that I will face? (Besides the paradigm change and the difficulty to evaluate performance due to lazy evaluation).
Immutable objects is the toughest hurdle.
Often you'll wind up calculating values that create new objects instead of updating existing objects. The idea that it's a mutable attribute of an object is a hard mental habit to break.
A derived property or method function is a better approach. Stateful objects are a hard habit to break.
With so many functional programming languages out there, how would you choose the one the better suit your needs?
It doesn't matter at first. Pick any language to learn. Once you know something, you're in a position consider picking another to better suit your needs.
I've read up on Haskell just to understand the things Python lacks.
Is Javascript a functional language? I know it has objects & you can do OOP with it also, but is it also a functional language, can it be used in that way?
Sometimes, people will say functional programming, when what they mean is imperative programming or procedural programming. Strictly speaking, functional programming is:
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.
Although Javascript is not widely known or used as a functional language, it does have some functional elements:
JavaScript has much in common with Scheme. It is a dynamic language. It has a flexible datatype (arrays) that can easily simulate s-expressions. And most importantly, functions are lambdas.
Scheme is a dialect of Lisp, and probably one of the languages most programmers think of when they discuss functional programming. When it comes to object orientation, Javascript is an object oriented language. But its object orientation is prototype based:
Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as classless, prototype-oriented or instance-based programming. Delegation is the language feature that supports prototype-based programming.
So although Javascript is object oriented, it doesn't follow the more common class based model, as do languages as C++, C#, Java and PHP (and quite a few others). And of course it's also an imperative language, which leads to the confusion with functional programming I described above.
You know how OOP became/seems like the next evolution in programming, does that mean that 'Functional Programming' is the next evolution
Object orientation and functional programming are just two of the many different programming paradigms, they are different styles of programming with different concepts and abstractions. The key word is "different". There isn't a single paradigm that's better than others or more evolved than others, each and every one fits some scenarios better than the others. Some may be quite older in origin than others, but in evolutionary terms that makes them better, as they have survided longer. But that's not a very smart way of looking at it.
Javascript, as I described above and as quite a few other languages, is multi-paradigm. It allows you to write code in imperative, prototype based object oriented and functional style. It's up to you to choose which one best fits whatever you are building. There are also several single paradigm languages, the canonical example being Java, which only allows for class based object oriented programming1.
You should really resist any urge to treat languages & paradigms as fashion statements. There's an abudance of crap out there, mostly written by fanboys / fangirls or marketing people, with little (if any) knowledge and understanding of programming. Terms like "better", "more evolved" etc, simply don't apply.
I learn best through examples, maybe someone could show performing the same task in a OOP way & then in a Functional Programming way for myself to understand & compare what functional programming does/is.
That would be a terrible way to learn. Functional and object orientation are quite different styles, and any example other than terribly simple ones would not fit one or the other style.
1 But lately tries to expand its scope to generic programming, let's see how that goes.
In conclusion:
- Concentrate on learning Javascript, it's a beautiful and extremly useful language. Learn the language, not the hype.
- Quite a few different paradigms, all equally useful. Up to you to choose which one you prefer and which one fits best whatever you're building.
- If you want to learn functional programming, choose a more suited language, like Scheme or Clojure. But you'll first need to understand the mathematical concepts involved.
- Do some research before you ask. Most of your questions are answered by the relevant Wikipedia articles. Knowing how to research and how to ask is an extremely important skill for any programmer.
Best Answer
Here is the deal.
1) You can use JavaScript as a functional language. I do so every day! its a great way to write code in JavaScript. The array operations in recent versions of JavaScript and the fact that the JavaScript function has all the power of a lambda in other languages make this easy to do. I would not call JavaScript a functional language but would call it a multi para-dime language.
2) There is only one thread of operation in JavaScript, however many things including Ajax operate in an Asynchronous manor. So when you make an Ajax call it returns right away and when server returns a value it sends an event that contains the return value.
3) JavaScript also lets you plan events for some time in the future. So you can tell it "In 500ms run this function"
There is a chapter on functional javascript in Programming HTML5 Applications this will be out in August. There should be a preview version of the book out sooner.