没有合适的资源?快使用搜索试试~ 我知道了~
该文档是国外的Brandon Skerritt写的python函数式编程教科书,十分钟完全可以学会,一起加油!
资源推荐
资源详情
资源评论
2018/8/18 Learn Functional Python in 10 Minutes – Hacker Noon
https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f 1/16
ApplausefromJamesLe,AakashNain,and889others
BrandonSkerritt
Follow
Iwriteaboutobscurecomputersciencetopicsthatareextremelyinteresting.Followmeto
learnmore.https://skerritt.tech
Jul30 · 10minread
LearnFunctionalPythonin10Minutes
In this article, you’ll learn what the functional paradigm is as well as
how to use functional programming in Python. You’ll also learn about
list comprehensions and other forms of comprehensions.
. . .
Functionalparadigm
In an imperative paradigm, you get things done by giving the computer
a sequence of tasks and then it executes them. While executing them, it
can change states. For example, let’s say you originally set A to 5, then
later on you change the value of A. You have variables in the sense that
the value inside the variable varies.
In a functional paradigm, you don’t tell the computer what to do but
rather you tell it what stu is. What the greatest common divisor of a
number is, what the product from 1 to n is and so on.
PhotobyChrisRiedonUnsplash
2018/8/18 Learn Functional Python in 10 Minutes – Hacker Noon
https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f 2/16
Because of this, variables cannot vary. Once you set a variable, it stays
that way forever (note, in purely functional languages they are not
called variables). Because of this, functions have no side eects in the
functional paradigm. A side eect is where the function changes
something outside of it. Let’s look at an example of some typical Python
code:
The output for this code is 5. In the functional paradigm, changing
variables is a big no-no and having functions aect things outside of
their scope is also a big no-no. The only thing a function can do is
calculate something and return it as a result.
Now you might be thinking: “no variables, no side eects? Why is this
good?”. Good question, gnarly stranger reading this.
If a function is called twice with the same parameters, it’s guaranteed
to return the same result. If you’ve learnt about mathematical
functions, you’ll know to appreciate this benet. This is called
referential transparency. Because functions have no side eects, if you
are building a program which computes things, you can speed up the
program. If the program knows that func(2) equates to 3, we can store
this in a table. This prevents the program from repeatedly running the
same function when we already know the answer.
Typically, in functional programming, we do not use loops. We use
recursion. Recursion is a mathematical concept, usually, it means
“feeding into itself”. With a recursive function, the function repeatedly
calls itself as a sub-function. Here’s a nice example of a recursive
function in Python:
1
2
3
4
5
6
a = 3
def some_func():
global a
a = 5
some func()
1
2
3
4
5
6
def factorial_recursive(n):
# Base case: 1! = 1
if n == 1:
return 1
# Recursive case: n! = n * (n-1)!
2018/8/18 Learn Functional Python in 10 Minutes – Hacker Noon
https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f 3/16
Some programming languages are also lazy. This means that they don’t
compute or do anything until the very last second. If you write some
code to perform 2 + 2, a functional program will only calculate that
when you actually need to use the resultant. We’ll explore laziness in
Python soon.
. . .
Map
To understand map, let’s rst look at what iterables are. An iterable is
anything you can iterate over. Typically these are lists or arrays, but
Python has many dierent types of iterables. You can even create your
own objects which are iterable by implementing magic methods. A
magic method is like an API that helps your objects become more
Pythonic. You need to implement 2 magic methods to make an object
an iterable:
The rst magic method, “__iter__” or dunder iter (double underscore
iter) returns the iterative object, this is often used at the start of a loop.
Dunder next returns what the next object is.
Let’s go into a quick terminal session and check this out:
This will print
1
2
3
4
5
6
7
8
9
10
11
12
13
class Counter:
def __init__(self, low, high):
# set class attributes inside the magic metho
# for "inistalise"
self.current = low
self.high = high
def __iter__(self):
# first magic method to make this object iter
return self
def __next__(self):
# d i th d
1
2
for c in Counter(3, 8):
print(c)
2018/8/18 Learn Functional Python in 10 Minutes – Hacker Noon
https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f 4/16
3
4
5
6
7
8
In Python, an iterator is an object which only has an __iter__ magic
method. This means that you can access positions in the object, but
cannot iterate through the object. Some objects will have the magic
method __next__ and not the __iter__ magic method, such as sets
(talked about later in this article). For this article, we’ll assume
everything we touch is an iterable object.
So now we know what an iterable object is, let’s go back to the map
function. The map function lets us apply a function to every item in an
iterable. Typically we want to apply a function to every item in a list,
but know that it’s possible for most iterables. Map takes 2 inputs, the
function to apply and the iterable object.
Let’s say we have a list of numbers like so:
[1, 2, 3, 4, 5]
And we want to square every number, we can write code like this:
Functional functions in Python are lazy. If we didn’t include the “list()”
the function would store the denition of the iterable, not the list itself.
We need to explicitly tell Python “turn this into a list” for us to use this.
v
ie
w r
a
w
1
i
❤
i
map(function, iterable)
1
2
3
4
x = [1, 2, 3, 4, 5]
def square(num):
return num*num
i (li ( ( )))
剩余15页未读,继续阅读
资源评论
小亮PlayNLP
- 粉丝: 230
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功