`
hlzhao
  • 浏览: 26997 次
  • 性别: Icon_minigender_1
  • 来自: 常州
最近访客 更多访客>>
社区版块
存档分类
最新评论

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/

分享到:
评论

相关推荐

    Introducing Erlang: Getting Started in Functional Programming

    Introducing Erlang: Getting Started in Functional Programming by Simon St. Laurent English | 6 Mar. 2017 | ASIN: B06XHSP5SH | 212 Pages | AZW3 | 1.85 MB If you’re new to Erlang, its functional style...

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    Getting Started with Functional Programming in Swift Chapter 3. The Swift programming language Chapter 4. Summary Chapter 5. First-class functions Chapter 6. Higher-order functions Chapter 7. ...

    Learning C++ Functional Programming

    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 ...

    Python Programming Blueprints 2018

    Get started with cloud computing by building microservice and serverless applications in AWS Develop scalable and cohesive microservices using the Nameko framework Create service dependencies for ...

    Unity.5.From.Zero.to.Proficiency.B01EJCE85M

    and quite often, regardless of your experience with games, it is sometimes difficult to find the time and motivation to get over the first barriers and to get started. Often, these barriers seem ...

    Packt.Python.Journey.from.Novice.to.Expert.2016

    The aim is to create a smooth learning path that will teach you how to get started with Python and carry out expert-level programming techniques at the end of course. Table of Contents Module 1: ...

    Introduction to JVM Languages

    If you want to build a strong foundation with the JVM and get started with popular modern programming languages, then this book is for you. It begins with a general introduction to JVM and the ...

    Swift.Programming.Nuts.and.Bolts.B01604F20C

    This book was written to help you acquire this knowledge by answering the following questions: 1) What are the general features and purpose of Swift, 2) Why you should use it (versus other programming...

    Mastering The Faster Web with PHP, MySQL, and JavaScript 1st pdf

    Along the way, you will learn how to profile your PHP scripts with Blackfire.io, monitor your Web applications, measure database performance, optimize SQL queries, explore Functional JavaScript, boost...

    Beginning.Ruby.From.Novice.to.Professional.3rd.Edition.1484212797

    How to do network programming with Ruby and more Who This Book Is For This book is for beginning programmers, programmers new to Ruby, and web developers interested in learning and knowing the ...

    Learning.Django.Web.Development

    This book is for web developers who want to get started with Django for web development. Basic knowledge of Python programming is required but no knowledge of Django is expected. In Detail Django, ...

    Unity.5.x.2D.Game.Development.Blueprints

    Get started with a quick overview of the principle concepts and techniques needed for making 2D games with Unity, and then dive straight in to practical development. Build your own version of Super ...

    Test-Driven iOS Development with Swift

    Learn how to incorporate all of the principles of test-driven development (TDD) in to your daily programming workflow Book Description Test-driven iOS Development with Swift will help you understand...

    C# Game Programming Cookbook for Unity 3D - 2014

    1.1 Important Programming Concepts.....................................2 1.1.1 Manager and Controller Scripts...............................2 1.1.2 Script Communication...................................

    Tkinter GUI Application Development HOTSHOT(PACKT,2013)

    Once you get started, you will be surprised to see how a few lines of code can produce powerful GUI applications. Tkinter GUI Application Development Hotshot helps you learn the art of GUI ...

    C++ Data Structures and Algorithms

    Know how to use arrays and lists to get better results in complex scenarios Build enhanced applications by using hashtables, dictionaries, and sets Implement searching algorithms such as linear search...

    Scala for Java Developers

    started and feel comfortable with both the syntax of the language and the tools. We will achieve this by exploring progressively some of the new concepts brought by Scala, in particular, how to unify ...

    geoprocessing scripts with python.pdf

    To get started with Python for geoprocessing, consider the following steps: 1. **Understanding Python Basics**: Before diving into geoprocessing, it's beneficial to have a basic understanding of ...

Global site tag (gtag.js) - Google Analytics