函数式编程(Functional Programming)

函数式编程已经有比较长的历史了,如今的动态语言,很大程度上也受到了函数式编程(反过来名叫命令式编程)的启发。

在函数式编程语言中,当你写了一个函数,接受一些参数,那么当你调用这个函数时,影响函数调用的只可能是你传进去的参数,而你得到的也只能够是计算结果。因此,一个void的方法,是没有任何意义的。如果传入了引用类型的参数,也是不合要求的。

在函数式编程中访问状态是十分安全的,因为状态不会改变,我可以把一个Point或List对象交给任意多的地方去访问,完全不用担心副作用。函数式编程的十分容易并行,因为我在运行时不会修改状态,因此无论多少线程在运行时都可以观察到正确的状态。两个函数完全无关,因此它们是并行还是顺序地执行便没有什么区别了。我们还可以有延迟计算,可以进行Memorization,经常使用递归作为控制流,这些都是函数式编程中十分有趣的方面。

在函数编程中,编程人员有一个天然框架用来开发更小的、更简单的和更一般化的模块, 然后将它们组合在一起。函数编程的一些基本特点包括:

  • 支持闭包和高阶函数。
  • 支持懒惰计算(lazy evaluation)。
  • 使用递归作为控制流程的机制。
  • 加强了引用透明性。
  • 没有副作用。

在实际编程中,我们需要共享许多东西,比如显示器,比如打印机,比如某一块区域的资源,一旦这样的事情发生(这样的事情太频繁了,以至于函数式编程反而变得越来越稀缺),我们就需要额外地考虑很多因素。

老赵写了这么一个有趣的说明:

private int i = 0;

public lambda int Function( int p, Random random )//编译错误,不允许引用类型参数
{
  int j = p + i;//编译错误,不允许使用i。
  p++;//编译错误,不允许更改变量
  int r = random.Next( j );//编译错误,不允许使用非lambda修饰的函数。
  return r;
}

对于非常讨厌在脚本语言代码中大片大片地使用全局的或者共享变量来传值、存储状态的人来说,使用函数式编程的递归和回调思想,可以把很多丑陋的代码优美起来。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learning C++ Functional Programming by Wisnu Anggoro English | 10 Aug. 2017 | ISBN: 1787281973 | ASIN: B06WVD7CVT | 304 Pages | AZW3 | 2.4 MB Key Features Modularize your applications and make them highly reusable and testable Get familiar with complex concepts such as metaprogramming, concurrency, and immutability A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases Book Description Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application's performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis. What you will learn Get to know the difference between imperative and functional approaches See the use of first-class functions and pure functions in a functional style Discover various techniques to apply immutable state to avoid side effects Design a recursive algorithm effectively Create faster programs using lazy evaluation Structure code using design patterns to make the design process easier Use concurrency techniques to develop responsive software Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization About the Author Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net. Table of Contents Diving into Modern C++ Manipulating functions in functional programming Applying immutable state to the function Recurring method invocation using recursive algorithm Procrastinating the execution process using Lazy Evaluation Optimizing code with Metaprogramming Running parallel execution using Concurrency Creating and debugging application in functional approach

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值