`
燮羽天翔
  • 浏览: 110344 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Apply Functional Programming Principles

阅读更多

     Edward Garson

 

     函数式编程最近又重新引起了主流编程社区的兴趣。一部分原因来自于函数式编程范式中聚合性质(emergent properties)的出现非常好的解决了由于我们的工业逐渐向多核转变所带来的挑战。但是,虽然可以肯定的说那是一项非常重要的应用,这并不是这篇文章用来说服你去了解函数式编程的根本原因。

     对函数式编程范式的理解和掌握能很大程度上提高你在别的情景环境下编写代码的质量。如果你对其理解到了一定深度,并在实际中应用范式,你的设计能够展现出一个较高程度的引用透明(referential transparency)。(tip:which means that for any given input the method call could be replaced by its result without affecting the program’s semantics.)

     引用透明是一个非常值得拥有的性质:它暗示着函数对于相同的输入总是产生相同的结果,不论它是在哪儿或是什么时候被触发。即,函数的求值更少地依赖于--更理想化的说,更本不依赖于--可变状态的副作用。

      在命令式代码(imperative code)中一个引起缺陷的最主要原因可归于变量的易变性。每个阅读这些代码的人都会想要去探究为什么在某些特定情况下,某些某些产出的值并非所想象的那样。可视性语义(visibility semantics)能够帮助减轻和缓解这些潜伏(insidious)的缺陷,或者至少极大地缩小他们可以出现的场合,但是他们造成的真正的问题实际上应该在于采用过度的可变性的先天性设计。

      而且在这一点上,我们肯定没有从工业标准那里得到任何帮助。对面向对象的介绍和宣传对这种设计起了无声的宣传作用,因为他们经常展示由那些总是开心地互相调用赋值方法的生命周期有点长的对象构成的图所组成的例子,他们是非常危险的。

      但是,自从有了TDD, 特别是当你确定地“模拟角色,而不是对象”,不必要的易变性可以在设计中被剔除。

      最终结果就是这样一种设计,更多的更小的函数作用于被传入给他们的参数,责任被更好地分配给了他们,而不是这些函数去引用易变的成员变量。更少的缺陷会出现,同时,他们会更容易地去debug,因为在这种设计中去定位某个被引入的离群的值会非常容易,否则你就得去在某个产生错误赋值的上下文中进行推导演算。这些都意味着(adds up to)一个更高水平的引用透明,同时可以确定的是没有任何东西会比去学习一门函数式编程语言更能使你把这些思想深深地印刻到你的骨子里,因为在这些语言中,这样的计算模式完全就是标准化的(norm)。

       当然,并非这些方法在所有情况下都是最优的选择。比如,在面向对象系统中,这种编程风格总会在领域模型的开发中(比如,通过协作来打破(减轻?)业务规则的复杂性)比基于UI开发得到更好的效果。

       掌握函数式编程范式,这样你就能非常明智地将你所悟所学应用到别的领域。你的对象系统(一方面)会与引用透明的优点产生共鸣,同时也会超乎你想象的接近他们在函数式编程领域的具有同样功能的系统。实际上,有些人甚至会断言,从他们的最高处看下去,函数式编程和面向对象编程不过是相互之间的倒影,犹如阴与阳的一种计算形式。(a form of conputational yin and yang.)

 

燮羽 2010/5/22

分享到:
评论

相关推荐

    Functional Programming in C#

    You'll start by learning the principles of functional programming and the language features that allow you to program functionally. As you explore the many practical examples, you'll learn the power ...

    Functional Programming Using F#

    This comprehensive introduction to the principles of functional programming using F# shows how to apply basic theoretical concepts to produce succinct and elegant programs It demonstrates the role of ...

    Learning Underscore.js

    Understand and learn to apply functional programming principles using the built-in functions of Underscore.js Leverage and reuse Underscore.js-based code to create code that targets client, server, or...

    Learning.Underscore.js.178439381

    Understand and learn to apply functional programming principles using the built-in functions of Underscore.js Leverage and reuse Underscore.js-based code to create code that targets client, server, or...

    Functional Programming in C#(True PDF)

    Functional Programming in C# teaches you to apply functional thinking to real-world problems using the C# language. You'll start by learning the principles of functional programming and the language ...

    面向Java开发者的函数式编程

    Dean Wampler, Java expert and author of Programming Scala (O'Reilly), shows you how to apply FP principles such as immutability, avoidance of side-effects, and higher-order functions to your Java code...

    OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide(SYBEX,2014)

    Full coverage of functional programming and all OCA Java Programmer exam objectives OCA, Oracle Certified Associate Java SE 8 Programmer I Study Guide, Exam 1Z1-808 is a comprehensive study guide for...

    Packt.Reactive.Programming.in.Kotlin

    Chapter 1, A Short Introduction to Reactive Programming, helps you understand the context, thinking pattern, and principles of reactive programming. Chapter 2, Functional Programming with Kotlin and ...

    Java SE 8 Programmer I Study Guide: Exam 1Z0-808

    * Work confidently with operators, conditionals, and loops * Understand object-oriented design principles and patterns * Master functional programming fundamentals Table of Contents Chapter 1 Java ...

    Java Program Design: Principles, Polymorphism, and Patterns

    The book enhances the traditional design patterns with Java’s new functional programming features, such as functional interfaces and lambda expressions. The result is a fresh treatment of design ...

    java Program Design: Principles, Polymorphism, and Patterns

    enhances the traditional design patterns with Java's new functional programming features, such as functional interfaces and lambda expressions. The result is a fresh treatment of design patterns that ...

    Practical Probabilistic Programming(Manning,2016)

    The book covers functional-style programming for text analysis and using object-oriented models to predict social phenomena like the spread of tweets, and using open universe models to model real-...

    Clojure.for.Finance.1785289284

    Apply the Clojure programming language in financial analytics and to build financial applications Work with high-level mathematical abstractions without having to implement low-level code for ...

    Scala:Applied Machine Learning

    After a quick refresher on functional programming concepts using REPL, you will see some practical examples of setting up the development environment and tinkering with data. We will then explore ...

    How.to.Become.a.Csharp.Programmer.B00QJ2TNB0.epub

    Preview introduced programmers to a new era called functional programming. C# 3.0 focused on bridging the gap between programming languages and databases. This book covers all the language features ...

    Adobe.Press.CSS.Animations.and.Transitions.for.the.Modern.Web.0133980502.pdf

    date information on current browser support for CSS animations and transitions An introduction to manipulating web ...principles for creating realistic animation and how they apply to web page designs

Global site tag (gtag.js) - Google Analytics