Just because functions are first class objects, there are closures, and higher order functions, does Javascript deserve to be called a Functional Programming language? The
As we know the functional programming language doesn't allow to change or mutate the elements(state)of functions but in javascript it is allowed in that sense it is not a functional programming language, although it does treat function as first class citizens.
I tend not to think of programming languages as having one particular paradigm, but that they lend themselves to certain paradigms. However just because they lend themselves to a particular paradigm doesn't mean you have to use that paradigm. It's quite possible to write object oriented programs in C and write imperative programs in ML. Not using a certain paradigm to solve a problem because the language isn't designed for it is just artificially limiting yourself (of course you should still take into account the limitations of a language when deciding if a particular solution will be a good solution).
In Javascript, you can do something like this!!
// Data
var fruits = [
{ name: 'apple', price: 5 },
{ name: 'orange', price: 10 },
{ name: 'lemon', price: 15 }
]
// Request Data from magicURL
request('magicURL')
.then(selectKeyOf('price'))
.then(priceMethod('sum'))
.then((result)=>{
console.log(result) // 30
})
I have made a github page in order to demo this concept and you can clone/view my implementation
Repeating my own answer to a similar question,
There's no accepted definition of functional programming language.
If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language.
If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, JavaScript *is not* a functional language.
I'd encourage you to read the following related blog posts (and also the comments below them):
Scala is not a functional language
Erlang is not functional
Reddit discussion on "Erlang is not functional"
Well, I wouldn't say it's functional programming, but then I would say it's object oriented and just today a friend said he wouldn't put it on that shelf either.
So, while I wouldn't say it is, I guess there's room for opinion. It does have classical features of functional programming, it doesn't have others.
@petraszd I rewrite your code a little to obtain a "new" for operator:
function ffor(a, b, f){
function it(i){
if(i > b)return
f(i)
it(i+1)
}
it(a)
}
print("----" + new Date()+"----")
var funcs = []
ffor(0, 9, function(i){
funcs.push(function(){return i})
})
ffor(0, 9, function(i){
print(funcs[i]())
})
But I know that this way has disadvantages for big loops...
Related question about tail recurtion optimization in JS
P.S. Posted here cuz have problem with code formatting while posting as comment