Is Javascript a Functional Programming Language?

2019-01-12 14:52发布

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 main thing I think it lacks is Pure Functions, and it doesn't 'feel' like other functional languages, like lisp (although thats not really a good reason for it not to be a functional langauge...)

13条回答
唯我独甜
2楼-- · 2019-01-12 15:20

if you stretch and twist the term "functional programming" to the point of philosophical discussions, this question may be open again. However, then you end up on the level of useful questions like "Is C++ really a programming language"?

The answer to your question on more daily level is "no".

Functional programming means that the program is conceptualized as a evaluation of a function, rather than a control flow. The code is a description of functions, and has no inherent concept of a control flow.

JavaScript has got a control flow and is conceptualized as a imperative language. From its design objective, it is clearly not a functional language.

查看更多
迷人小祖宗
3楼-- · 2019-01-12 15:26

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

查看更多
聊天终结者
4楼-- · 2019-01-12 15:27

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

查看更多
在下西门庆
5楼-- · 2019-01-12 15:29

I would say that it is a multi-paradigm language.

EDIT: It's multi-paradigm and includes functional constructs.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-12 15:30

What I really hate in javascript (if You try to look at it as FP language) is this:

function getTenFunctionsBad() {
  var result = [];
  for (var i = 0; i < 10; ++i) {
    result.push(function () {
      return i;
    });
  }
  return result;
}

function getTenFunctions() {
  var result = [];
  for (var i = 0; i < 10; ++i) {
    result.push((function (i) {
      return function () {
        return i;
      }
    })(i));
  }
  return result;
}

var functionsBad = getTenFunctionsBad();
var functions = getTenFunctions()
for (var i = 0; i < 10; ++i) {
  // using rhino print
  print(functionsBad[i]() + ', ' + functions[i]());
}

// Output:
//   10, 0
//   10, 1
//   10, 2
//   10, 3
//   10, 4
//   10, 5
//   10, 6
//   10, 7
//   10, 8
//   10, 9

You need to understand JS stack environment (I don't if it is the right term) to understand such a behavior.

In scheme for example You just can't produce such thing (Ok, ok -- with the help of underlying languages' references You can make it):

(define (make-ten-functions)
  (define (iter i)
    (cond ((> i 9) '())
          (else (cons (lambda () i) (iter (+ i 1))))))
  (iter 0))

(for-each (lambda (f)
            (display (f))
            (newline)) (make-ten-functions))
查看更多
混吃等死
7楼-- · 2019-01-12 15:31

The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:

  1. Has first-class functions
    • Javascript is this!
  2. Is based on functions as used in the lambda calculus, with an emphasis on avoiding persistent mutable state (often replacing it with parameters passed to functions)
    • As commonly written, Javascript is not remotely this!

Pick your meaning and then the question is answerable.

查看更多
登录 后发表回答