How to get started with functional programming

本文介绍了如何通过编写纯函数开始学习函数式编程。纯函数只依赖其输入参数,并且总是给出相同的输出结果,使得它们易于理解和调试。文章还讨论了纯函数在实际编程中的应用和限制。

by John on July 24, 2011


Someone asked me this weekend how to get started with functional programming. My answer: Start by writing pure functions in the programming language you’re currently using.

The only input to a pure function is its argument list and the only output is its return value. If you haven’t seen this before, you might think all functions are pure. After all, any function takes in values and returns a value. But in conventional programming there are typically out-of-band ways for information to flow in or out of a function. For example, an impure function may depend on a global variable or class member data. In that case, it’s behavior is not entirely determined by its arguments. Similarly, an impure function might set a global variable or write to a database. In that case the function has a side effect in addition to its return value.

You can write pure functions in any language, though it’s easier in some languages than others. For example, no one would call Fortran a functional language, but there are people (M. J. D. Powell comes to mind) who discipline themselves to write pure functions in Fortran.

Why write pure functions? A pure function has referential transparency, meaning it will always return the same value when given the same inputs. The output does not depend on the system time, the state of a database, which functions were called previously, or anything else that is not explicitly passed as an argument to the function. This means pure functions are easier to understand (and hence easier to debug and test).

You can’t always write pure functions. If you need to stick a value in a database, this cannot be accomplished with a pure function. Or if you’re calling a random number generator, you don’t want it to have referential transparency, always returning the same output! But the goal is to use pure functions when practical. You want to eliminate out-of-band communication when it’s convenient to do so. Total purity is not practical; some argue that the sweet spot is about 85% purity.

So why don’t programmers use pure functions more often? One reason is that pure functions require longer argument lists. In an object oriented language, object methods can have shorter argument lists by implicitly depending on object state. The price to pay for shorter method signatures is that you can’t understand a method by itself. You have to know the state of the object when the method is called. Is it worthwhile to give up referential transparency in order to have shorter method signatures? It depends on your context and your taste, though in my opinion its often worthwhile to use longer function signatures in exchange for more pure functions.

Another reason people give for not writing pure functions is that its too expensive to copy large data structures to pass them into a function. But that’s what pointers are for. You can conceptually pass an object into a function without having to actually make a copy of the object’s bits.

You can also fake purity for the sake of efficiency. For example, Mike Swaim left a comment recently giving an example of how memoization sped up a program by several orders of magnitude. (Memoization is a technique of caching computations. When a function is asked to compute something, it first looks to see whether it has already done the calculation. If so, it returns the cached value. If not, it does the calculation and adds its output to the cache.) A function that uses memoization is not strictly pure — its calculations have a persistent impact on the state of its cache — but such a function can still have referential transparency, always returning the same output given the same input. You could say it’s cheating to call such functions pure, and it is, but if you’re really a stickler about it, all pure functions have side effects.

 

 

From : http://www.johndcook.com/blog/2011/07/24/get-started-functional-programming/

下载代码方式:https://pan.quark.cn/s/e6c2e312b658 在苹果公司的Mac操作系统环境中,当用户尝试安装非原厂驱动程序时,可能会遭遇系统无法正常启动的困境。这种情况常常源于名为.kext的内核扩展驱动程序存在兼容性问题或安装过程中出现失误。这份指南介绍了一种无需重新安装操作系统且能够保护所有用户数据的修复方法,这一方案对于先前许多面临类似挑战的用户而言,曾是极为棘手的情况。文档中提及的“用户模式启动”实际是指单用户模式,这种启动方式仅加载核心系统功能,而忽略图形用户界面及常规应用程序的加载。在单用户模式下,用户能够访问命令行界面,进而执行一系列修复指令。解决此问题的首要环节是验证存储设备是否存在故障,因为这是导致系统无法启动的常见诱因。借助终端指令`/sbin/fsck -f`,可以诊断并纠正文件系统层面的错误。倘若系统在启动过程中检测到文件系统异常,通常会自动执行`fsck`命令,然而,如果系统卡在进度条100%无法继续,手动运行该命令则显得尤为必要。指令`mount -uw /`的功能是将根目录切换为可读写状态,由于系统默认是以只读模式启动的。这一操作的目的是为了在不重新进入正常模式的前提下,对系统进行必要的调整。随后,文档提供了一个关键操作:对存在问题的驱动程序文件进行修改或更名。在Mac系统中,第三方驱动程序一般安装在`/Library/Extensions/`目录下。每个驱动程序都包含一个以.kext为后缀名的文件夹,例如在此案例中的AX88772.kext。通过命令行将故障的.kext文件更名(例如改为.kext.bak),可以临时禁用该驱动程序。这一操作需在命令行环境中完成,首先使用`cd /Library/Exte...
源码链接: https://pan.quark.cn/s/a4b39357ea24 海康威视NVR76/78N系列是一款为中小型企业及家庭用户量身打造的网络视频录像机(Network Video Recorder),其核心用途在于对来自网络摄像头的视频流进行管理和存储。该系列具备兼容萤石云服务的功能,这使得用户能够通过网络远程进行监控系统的访问、监控以及管理,借助互联网达成随时随地进行视频查看和录像回放的目标。在标题中提及的"海康NVR76/78N系列升级包"具体指代适用于这一系列录像机的软件更新套装。此类升级通常涵盖性能改进、新增功能、安全漏洞修正以及兼容性增强等多个方面,旨在保障设备始终处于最新状态,优化运行效能并改善用户使用感受。譬如,升级或许囊括了更为先进视频压缩技术的应用,用以降低网络带宽的消耗,或者为新型网络摄像头提供支持。产品说明中所列出的型号,如DS-7816N-SNH、DS-7816N-SHT、DS-7816N-SHT/N、DS-7816N-SHT/P,均属于海康威视NVR76/78N系列的特定版本。这些型号之间的不同之处可能体现在硬盘接口数量、视频通道数目、视频解码性能、网络接口规格以及是否集成内置电源等硬件参数。型号中的"DS-7816N"表明该设备能够同步处理16个视频通道,而后续的字母标识及后缀则可能象征着不同的功能或特性。"萤石云"是由海康威视开发的一套云服务解决方案,它提供了远程监控、即时视频浏览、录像保管和智能警报等多项服务。用户可借助萤石云App或网页版,便捷地监管和检视自身的监控装置,不受地理位置限制。相较于传统的本地存储方式,萤石云服务提供了一种更为方便且可靠的数据备份途径,特别是在拥有多个监控点或需防止设备失窃的情境下...
下载代码方式:https://pan.quark.cn/s/a4b39357ea24 在移动应用开发领域中,Android平台与Unity3D引擎的整合是一项普遍存在的技术需求,特别是在游戏开发以及混合应用构建方面。本示例演示了如何在Android原生代码和Unity3D引擎之间建立高效的通信机制,以完成数据传递和功能执行的任务。为了有效运用此技术,必须充分认识Android和Unity3D各自的技术特性。 Android作为一个开源的移动操作系统,它提供了大量的应用程序接口和开发工具,适用于构建原生应用程序。而Unity3D则是一款支持多平台的游戏开发工具包,能够用来设计二维、三维游戏以及交互式体验。Unity3D具备卓越的图形渲染性能和便捷的编程接口,但有时需要与Android系统的功能相融合,例如接入硬件设备、管理系统级事件等。 当在Android设备上启动Unity3D应用时,一般通过UnityPlayer类来完成。UnityPlayer是Unity系统在Android设备上的连接媒介,能够用来运行Unity的编程代码或获取Unity的用户界面。举例来说,可以定义一个Intent对象,将信息打包后经由UnityPlayer传输给Unity,然后在Unity的C#编程环境中接收并处理这些信息。 相对于Unity3D调用Android系统,通常需要借助Unity的插件架构。开发者需编写Java语言编写的Android插件,并实现特定的程序接口,随后在Unity环境中使用DllImport指令导入这个插件。Unity会自动将Java代码编译并整合到工程中。执行时,通过Unity的DllImport函数调用Android插件的方法,从而执行Android设备上...
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 本文阐述了新型无线编码芯片EV1527在无线发射模块中的应用及其对应解码方法在无线接收模块中的达成。首先对编码芯片EV1527的操作进行了概述;其次阐述了两种解码方案:借助解码芯片TDH6300进行硬件解析、运用单片机执行软件解码;最后详细地展示了这种编解码系统的实施。 ### EV1527编码芯片的应用及其解码方案 #### 一、引言 随着技术的革新,无线控制技术正迅速发展并得到普遍采用。常规的无线控制装置(如2262发射装置与2272接收装置)尽管应用广泛,但由于其硬件设定的地址码容易被仿制,存在显著的安全风险。与此形成对比的是,EV1527作为一种创新的无线编码芯片,能够提供更为安全且可靠的解决方案。本文将深入探讨EV1527编码芯片的应用及其相关解码策略。 #### 二、EV1527编码芯片概述 **1. EV1527的优势** - **不可仿制性:** EV1527内置有20位可预编程内码,理论上能够产生100万种不同的内码组合,这极大降低了编码重复的可能性。 - **自学习功能:** 发送与接收模块之间能够通过自学习过程完成配对,即便发射模块遗失,也能通过重新学习使原发射模块失效,从而增强安全性。 - **节能特性:** 在无按键触发时,芯片进入低功耗模式,有助于节约能源。 **2. EV1527的引脚功能** - **第5-8引脚:** 按键输入引脚,用于识别用户的操作。 - **第4引脚:** 数据输出引脚,用于传输经过编码的信号。 - **第1引脚:** 振荡电阻连接引脚,用于调节振荡周期。 - **第2引脚:** 电源输入引脚。 #### 三、发射模块的实...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值