Javascript 是函数式编程语言吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3962604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Javascript a Functional Programming Language?
提问by hvgotcodes
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...)
仅仅因为函数是一等对象、有闭包和高阶函数,Javascript 就应该被称为函数式编程语言吗?我认为它缺少的主要内容是 Pure Functions,它不像其他函数式语言,如 lisp(尽管这并不是它不是函数式语言的真正好理由......)
回答by missingfaktor
Repeating my own answerto 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):
函数式编程语言没有公认的定义。
如果您将函数式语言定义为支持一等函数和 lambda 的语言,那么是的,JavaScript *是*一种函数式语言。
如果您还考虑诸如支持不变性、代数数据类型、模式匹配、部分应用等因素,那么不,JavaScript * 不是 * 函数式语言。
我鼓励您阅读以下相关的博客文章(以及它们下面的评论):
回答by Niki Yoshiuchi
I would say that it is a multi-paradigm language.
我会说它是一种多范式语言。
EDIT: It's multi-paradigm and includes functional constructs.
编辑:它是多范式,包括功能结构。
回答by shuhalo
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"?
如果您将“函数式编程”一词延伸到哲学讨论的角度,那么这个问题可能会再次开放。但是,您最终会遇到一些有用的问题,例如“C++ 真的是一种编程语言吗”?
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.
JavaScript 有一个控制流,并被概念化为一种命令式语言。从其设计目标来看,它显然不是一种函数式语言。
回答by Chuck
The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:
如今,术语“函数式编程”语言如此重载,几乎毫无用处。有两个主要含义:
- Has first-class functions
- Javascript is this!
- 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!
- 拥有一流的功能
- Javascript是这样的!
- 基于 lambda 演算中使用的函数,重点是避免持久可变状态(通常用传递给函数的参数替换它)
- 正如通常所写的,Javascript 不是远程的!
Pick your meaning and then the question is answerable.
选择你的意思,然后这个问题就可以回答了。
回答by marknery
回答by David Brown
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).
我倾向于不认为编程语言具有一种特定的范式,而是认为它们适用于某些范式。然而,仅仅因为它们适用于特定范式并不意味着您必须使用该范式。很可能用 C 编写面向对象的程序并用 ML 编写命令式程序。不使用某种范式来解决问题,因为该语言不是为它设计的,这只是人为地限制了自己(当然,在决定特定解决方案是否是一个好的解决方案时,您仍然应该考虑语言的局限性)。
回答by Brian Onn
To me, Javascript is both an imperative language and a functional language, and you can choose to use it either way, and even (egad) both ways. Or you can choose to use one paradigm and never touch the other. It's up to you. I, like you, don't think Javascript should be calleda Functional Language, because it allows you to wander in and out of the functional programming paradigm. Perhaps if it had a pragma of some kind, to limit you using only functional programming paradigms, then that would be useful, I think. But, in summary, I say it's more of a imperative/procedural language with some functional programming features tossed in.
对我来说,Javascript 既是命令式语言又是函数式语言,您可以选择使用任何一种方式,甚至 ( egad) 两种方式。或者,您可以选择使用一种范式而从不接触另一种范式。由你决定。我和你一样,不认为 Javascript 应该被称为函数式语言,因为它允许你在函数式编程范式中徘徊。也许如果它有某种编译指示,限制你只使用函数式编程范式,那么我认为这会很有用。但是,总而言之,我说它更像是一种带有一些函数式编程特性的命令式/过程式语言。
回答by Brian Onn
Javascript is to a point. It truly depends on how you go about programming it. If I code in an OO manner, would it not be OO? So if you just code things in a 'functional' manner it would be functional. I guess it is multi-paradigm language so to call it just one thing isn't entirely accurate.
Javascript是一个点。这实际上取决于您如何对其进行编程。如果我以面向对象的方式编码,它不是面向对象吗?因此,如果您只是以“功能性”方式编写代码,它将是功能性的。我猜它是多范式语言,因此仅将其称为一件事并不完全准确。
回答by slezica
Well, I wouldn't say it's functional programming, but then I wouldsay 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.
所以,虽然我不会说它是,但我想有意见的余地。它确实具有函数式编程的经典特征,但没有其他特征。
回答by Wayne Chiu
In Javascript, you can do something like this!!
在 Javascript 中,你可以做这样的事情!!
// 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 pagein order to demo this concept and you can clone/view my implementation
我制作了一个github 页面来演示这个概念,你可以克隆/查看我的实现