Is Javascript a Functional Programming Language?

前端 未结 13 846
天涯浪人
天涯浪人 2020-12-04 05:22

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

相关标签:
13条回答
  • 2020-12-04 05:34

    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.

    0 讨论(0)
  • 2020-12-04 05:38

    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).

    0 讨论(0)
  • 2020-12-04 05:38

    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

    0 讨论(0)
  • 2020-12-04 05:39

    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"

    0 讨论(0)
  • 2020-12-04 05:39

    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.

    0 讨论(0)
  • 2020-12-04 05:42

    @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

    0 讨论(0)
提交回复
热议问题