Functional programming-函数式编程
In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions[1] or declarations[2] instead of statements. In functional code, the output value of a function depends only on the arguments that are passed to the function, so calling a function f twice with the same value for an argument x produces the same result f(x) each time; this is in contrast to procedures depending on a local or global state, which may produce different results at different times when called with the same arguments but a different program state. Eliminating side effects, i.e., changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
Functional programming has its origins in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus. Another well-known declarative programming paradigm, logic programming, is based on relations.[3]
A number of concepts and paradigms are specific to functional programming, and generally foreign to imperative programming (including object-oriented programming). However, programming languages are often hybrids of several programming paradigms, so programmers using "mostly imperative" languages may have utilized some of these concepts.[40]
First-class and higher-order functions[edit]
Higher-order functions are functions that can either take other functions as arguments or return them as results. In calculus, an example of a higher-order function is the differential operator d/dx, which returns the derivative of a function f.
Higher-order functions are closely related to first-class functions in that higher-order functions and first-class functions both allow functions as arguments and results of other functions. The distinction between the two is subtle: "higher-order" describes a mathematical concept of functions that operate on other functions, while "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values).
Higher-order functions enable partial application or currying, a technique that applies a function to its arguments one at a time, with each application returning a new function that accepts the next argument. This lets a programmer succinctly express, for example, the successor function as the addition operator partially applied to the natural number one.
Pure functions[edit]
Pure functions (or expressions) have no side effects (memory or I/O). This means that pure functions have several useful properties, many of which can be used to optimize the code:
- If the result of a pure expression is not used, it can be removed without affecting other expressions.
- If a pure function is called with arguments that cause no side-effects, the result is constant with respect to that argument list (sometimes called referential transparency), i.e., if calling the pure function again with the same arguments returns the same result. (This can enable caching optimizations such as memoization.)
- If there is no data dependency between two pure expressions, their order can be reversed, or they can be performed in parallel and they cannot interfere with one another (in other terms, the evaluation of any pure expression is thread-safe).
- If the entire language does not allow side-effects, then any evaluation strategy can be used; this gives the compiler freedom to reorder or combine the evaluation of expressions in a program (for example, using deforestation).
While most compilers for imperative programming languages detect pure functions and perform common-subexpression elimination for pure function calls, they cannot always do this for pre-compiled libraries, which generally do not expose this information, thus preventing optimizations that involve those external functions. Some compilers, such as gcc, add extra keywords for a programmer to explicitly mark external functions as pure, to enable such optimizations. Fortran 95 also lets functions be designated pure.
https://en.wikipedia.org/wiki/Functional_programming
Functional programming-函数式编程的更多相关文章
- Python Lambda & Functional Programming
Python Lambda & Functional Programming 函数式编程 匿名函数 纯函数 高阶函数 # higher-order functions def apply_tw ...
- Python进阶【第五篇】函数式编程及某些特殊函数
一.函数式编程——Functional Programming 函数式=编程语言定义的函数+数学意义的函数 在计算机的层次上,CPU执行的是加减乘除的指令代码,以及各种条件判断和跳转指令,所以,汇编语 ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- Sth about 函数式编程(Functional Programming)
今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...
- iOS 开发之函数式编程思想(Functional Programming)
函数式编程(Functional Programming), 函数式编程强调的函数:1.不依赖外部状态:2.不改变外部状态. 函数式编程可解决线程安全问题,每一个函数都是线程安全的. 时间状态:变量一 ...
- 函数式编程 - Functional Programming
什么是函数式编程 函数式编程是一种编程范式. 编程范式又是什么? 编程范式是一种解决问题的思路. 命令式编程 把程序看作 一系列改变状态的指令: 函数式编程 把程序看作 一系列数学函数映射的组合. i ...
- [译]java8新特性:函数式编程(functional programming)的优点
Java8引入了函数式编程,他对java是一个极大的扩展.Java从此不在是一个单纯的面向对象语言,现在他同时混合了函数式编程.这是巨大的改变,需要我们调整面对对象的编程习惯,以适应这些变化. 但是为 ...
- 编程范式 --- 函数式编程(Funtional Programming,简称FP)
函数式编程(Funtional Programming,简称FP)是一种编程范式,也就是如何编写程序的方法论 主要思想:把计算过程尽量分解成一系列可复用函数的调用 主要特征:函数是"第一等公 ...
- Java中的函数式编程(二)函数式接口Functional Interface
写在前面 前面说过,判断一门语言是否支持函数式编程,一个重要的判断标准就是:它是否将函数看做是"第一等公民(first-class citizens)".函数是"第一等公 ...
随机推荐
- RHEL 7 & CentOS 7禁用IPV6(转载)
RHEL 7 & CentOS 7下禁用IPV6的方法和之前的版本不太一样了,本文整理了一下处理方法: 首先,我们必须给出最根本的解决方法:修改grub,在引导时就不加载IPV6模块 这样修改 ...
- BZOJ 1305: [CQOI2009]dance跳舞 网络最大流_二分答案_建模
Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...
- node——模块化
之前写的新闻部分几乎所有操作都写在了一起,这次开始进行模块化. 为什么要模块化: 1.提高开发效率,所有操作在一个文件内,不方便团队操作,模块化可多人同时操作 2.当程序出错,可以分模块寻找错误 3. ...
- 序列模型(3)---LSTM(长短时记忆)
摘自https://www.cnblogs.com/pinard/p/6519110.html 一.RNN回顾 略去上面三层,即o,L,y,则RNN的模型可以简化成如下图的形式: 二.LSTM模型结构 ...
- el7上的开机自动执行脚本
/etc/rc.local 是 /etc/rc.d/rc.local的软连接 默认, /etc/rc.local 是有可执行权限的, 只要 给 /etc/rc.d/rc.local 加上可执行权限即可 ...
- 解决time命令输出信息的重定向问题
解决time命令输出信息的重定向问题 time命令的输出信息是打印在标准错误输出上的, 我们通过一个简单的尝试来验证一下. [root@web186 root]# time find . -name ...
- android的listview的addheaderView总是出现空指针的错误
android的listview的addheaderView总是出现空指针的错误, 网上的处理方法如下: // This doesn't work... nullPointerException Li ...
- php程序员需要撑握的知识点
1. 基本知识点 HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码 ...
- nyoj124-中位数
中位数 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一组数据按从小到大的顺序依次排列,处在中间位置的一个数叫做中位数. 比如 1 5 10 11 9 其中位数就是9 ...
- vue项目使用简略总结
1.利用iView Cli搭建项目结构2.搭建完毕之后将proxy.js和'Server.js'放置到node_modules\webpack-dev-server\lib目录下,以实现跨域访问公司平 ...