`

也说Functional Programming

阅读更多
        看了Lu Feng兄写的4个程序员的一天以及很多朋友对此post的回复,不禁让我兴奋不已。因为我想起了Python,毕竟在Functional Programming方面又怎么能少了Python这条可爱的小蟒蛇呢?
        偶没有Lu Feng兄那么强的表达能力,能够将语言的特性如讲故事一般娓娓道来,只能随手写两行代码来展示一下Python在Functional Programming方面的功底了。:)
        首先,在Python中,Everything is object,所以function本身也是object,是当作一等公民(First class citizen)来对待的,也就是说一个函数本身可以作为另外一个函数的参数,也可以接受另外一个函数作为自己的参数。是 不是有点不分你我,人人平等的感觉呢?呵呵~~ 从Python有很多内建函数都可以看出其对于Functional Programming的支持。我们先来看一个例子:内建的filter函数。filter函数的作用是将一个序列(如List,String等)中符合要求的元素挑出来,那么这个要求通过什么来体现呢?就是通过一个返回布尔型的函数了,也就是说filter函数的其中一个参数是函数。filter函数的signature是filter (function, sequence),请看以下代码:

>>> name = 'perhaps'
>>> def f(x): return x > 'a' and x < 'p'

>>> filter(f, name)
'eh'

除了filter之外,还有map和reduce,有兴趣的朋友,可以参阅Python Tutorial。

        接着,我们再来看看Lu Feng兄在post中提到的那个例子。Scheme的代码真的很简单,简单得让我这个Python的热衷者都汗颜了。不过,我倒是有个疑问。根据文中用Scheme写的Foo函数,我在Python中,一样可以模仿着定义出来的:
   
>>> def Foo(op, x, y): op(x, y)

是 不是也如Scheme一样简单呢?但是,问题来了,Foo函数在调用的时候,op函数是不是也应该已经一并定义出来了呢?也许是Scheme对于运算符有 很好的支持吧,所以在Scheme中,可以使用(+, 1, 2)这样的方式。如果op变得复杂,是不是仍然会是这样的简单呢?对Scheme了解,我就局限于它是黑客的第一门语言,因此不敢再作推断了。好,根据以 上Foo函数的定义,我们来看看在Python中,如何调用这个Foo函数吧:

>>> Foo(lambda x, y: x+y, 12)
>>> 3

>>> def f(x, y): return x*y

>>> Foo(f, 36)
>>> 18

在以上的代码中,lambda会比较特别,它是用于定义匿名函数的。要在Python中用好Functional Programming,可少不了它哦。
         就说到这里了,对于Functional Programming有兴趣的朋友,可以看看这一篇文章:Functional Programming with Python 。
分享到:
评论

相关推荐

    Functional Programming pdf 2018

    Explore functional programming and discover new ways of thinking about code. You know you need to master functional programming, but learning one functional language is only the start. In this book, ...

    Functional Programming in C++ (2018.11出版,PDF格式)

    Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full ...

    Functional programming in Java.pdf

    With the introduction of Java 8, many functional programming techniques have been added to the language. However, functional programming may seem unfamiliar to developers who are used to using ...

    Functional Programming in C++

    Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full ...

    Functional Programming in Scala

    Functional programming (FP) is a programming style emphasizing functions that return consistent and predictable results regardless of a program's state. As a result, functional code is easier to test ...

    Functional Programming in C#

    Functional Programming in C# teaches you to apply functional thinking to real-world problems using the C# language. The book, with its many practical examples, is written for proficient C# programmers...

    monads for functional programming

    ### Monad在函数式编程中的应用 #### 引言 Monad作为一种源自范畴论的概念,在理论计算机科学领域得到了广泛的应用。尤其在函数式编程中,Monad提供了一种强大的工具,用于模拟其他语言中的各种副作用,例如全局...

    Learning C++ Functional Programming

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

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

    Real World Functional Programming: With Examples in F# and C#

    Functional programming languages like F#, Erlang, and Scala are attracting attention as an efficient way to handle the new requirements for programming multi-processor and high-availability ...

    Functional_Python_Programming

    Functional programming defines a computation using expressions and evaluation—often encapsulated in function definitions. It de-emphasizes or avoids the complexity of state change and mutable objects...

    Why Functional Programming Matters

    As software becomes more and more complex, it is ... We conclude that since modularity is the key to successful programming, functional programming offers important advantages for software development.

    Functional.Programming.in.JavaScript.1784398225

    Title: Functional Programming in JavaScript Author: Dan Mantyla Length: 152 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-03-26 ISBN-10: 1784398225 ISBN-13: ...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    The [removed] Functional Programming for JavaScript Developers course will take you on a journey to show how functional programming when combined with other techniques makes JavaScript programming ...

Global site tag (gtag.js) - Google Analytics