函数式编程
函数式编程是一种编程范式。什么叫范式?我的理解就是做事情的一种方法,做事情的一种规范。
函数式编程的一个重要特点就是,可以把函数本身作为参数传入另一个函数,也可以把函数作为返回结果。
在函数式编程中,函数是第一类对象,意思是说一个函数,既可以作为其它函数的输入参数值,也可以从函数中返回值,被修改或者被分配给一个变量。
函数式编程与命令式编程的区别
函数式编程与命令式编程最大的不同其实在于:
函数式编程关心数据的映射,命令式编程关心解决问题的步骤。
英文定义
其实我也喜欢维基百科里面的英文定义:
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.
In functional programming, functions are treated as first-class citizens, meaning that they can be bound to names (including local identifiers), passed as arguments, and returned from other functions, just as any other data type can. This allows programs to be written in a declarative and composable style, where small functions are combined in a modular manner.
Functional programming is sometimes treated as synonymous with purely functional programming, a subset of functional programming which treats all functions as deterministic mathematical functions, or pure functions. When a pure function is called with some given arguments, it will always return the same result, and cannot be affected by any mutable state or other side effects. This is in contrast with impure procedures, common in imperative programming, which can have side effects (such as modifying the program’s state or taking input from a user). Proponents of purely functional programming claim that by restricting side effects, programs can have fewer bugs, be easier to debug and test, and be more suited to formal verification.
Functional programming has its roots in academia, evolving from the lambda calculus, a formal system of computation based only on functions. Functional programming has historically been less popular than imperative programming, but many functional languages are seeing use today in industry and education, including Common Lisp, Scheme,Clojure, Wolfram Language,Racket,Erlang,Elixir, OCaml, Haskell,and F#. Functional programming is also key to some languages that have found success in specific domains, like JavaScript in the Web,R in statistics,J, K and Q in financial analysis, and XQuery/XSLT for XML.Domain-specific declarative languages like SQL and Lex/Yacc use some elements of functional programming, such as not allowing mutable values. In addition, many other programming languages support programming in a functional style or have implemented features from functional programming, such as C++11, Kotlin,Perl,PHP,Python,Go,Rust,Raku, Scala,and Java (since Java 8).
(完)