Skip to content

Commit b8e99ef

Browse files
committed
Fist post in functional programming in python series
The first post in the functional programming with python series, it covers the following topics: - Imperative vs Functional programming - Pure vs Impure functions
1 parent 87d9d98 commit b8e99ef

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
layout : post
3+
title: "An Introduction to Functional Programming with Python - Part 1"
4+
categories : functional python tutorial
5+
tags: blog
6+
author: Alex
7+
comments: false
8+
---
9+
10+
This is the first part of a tutorial series meant to introduce the functional programming paradigm
11+
using features built into Python. Most of what I will talk about will be available in both
12+
Python 2.7.x and Python 3.x however in this series I will be using Python 3.x and will point out any
13+
differences when needed.
14+
15+
I suppose I had better start by answering...
16+
17+
## What is a Paradigm?
18+
19+
It's a way of doing things and as you can see [here][paradigm-list] - there are a lot of them!
20+
21+
If you have completed Vince's [Computing for Maths][cfm] class then you will have been taught the Python
22+
programming language but using the [imperative][imperative-wiki] and [object oriented][oop-wiki] paradaigms.
23+
24+
Ok that's the fancy stuff out of the way, in plain English this means that you tell the computer what
25+
to do step-by-step for example consider the following bit of Python
26+
27+
{% highlight py3 %}
28+
29+
def calc_average(nums):
30+
"""
31+
This function calculates the average of a list of numbers, it assumes the
32+
list conatins only numbers and is non-empty
33+
34+
Arguments:
35+
- nums: A list of numbers
36+
37+
Outputs:
38+
- The average, a floating point number
39+
"""
40+
41+
sum = 0
42+
43+
for n in nums:
44+
sum += n
45+
46+
return sum/float(len(nums))
47+
{% endhighlight %}
48+
49+
Here we have to tell Python exactly what it needs to do verbaitum
50+
51+
1. Create a variable called sum, assign it the value zero.
52+
2. Go through each item in the list in turn and add it's value to the variable sum
53+
3. Divide that by the number of items there were in the list - oh! and
54+
don't forget to cast to a float
55+
56+
Whereas the functional paradigm is part of a family of [declarative programming][declarative-wiki]
57+
paradgims, where instead of detailing all the steps you simply 'declare' what something is or how to
58+
transform it into another form. In the case of [functional programming][functional-wiki] this is done
59+
(suprisingly) using functions , for example we can rewrite the above as the following.
60+
61+
{% highlight py3 %}
62+
63+
from functools import reduce
64+
65+
def f_average(nums):
66+
"""
67+
The same as the calc_averaage function, just in a functional style
68+
"""
69+
sum = reduce(lambda x, y: x + y, nums, 0)
70+
71+
return list(sum/float(len(nums)))
72+
{% endhighlight %}
73+
74+
> Python 2.7.x Differences
75+
>
76+
> The ```reduce``` function was only moved into ```functools``` in Python > 3.0 so
77+
> it doesn't need to be imported. Also the result doesn't require being cast to
78+
> a list.
79+
80+
Don't worry too much if you don't understand the code above we will get around to how exactly
81+
this works in later posts, the important point is to note that this style is slightly more
82+
abstract and we let python handle some more of the details for us.
83+
84+
## Pure vs Impure Functions
85+
86+
There are two types of functions in the world _pure_ and _impure_. Below are two functions - can
87+
you spot the pure function from the impure function?
88+
89+
{% highlight py3 %}
90+
91+
def f(x):
92+
return x + 1
93+
94+
def g(x):
95+
if isTuesday():
96+
return x + 1
97+
else:
98+
return x
99+
{% endhighlight %}
100+
101+
The function ```g``` is the impure function since it depends on the state of the system it is run on.
102+
Depending on if it is Tuesday or not this function may or may not do something, one of the main features
103+
of the functional paradigm and the functional programming langauges (such as Haskell) is that
104+
they deal in pure functions as far as possible.
105+
106+
The function ```f``` is pure, which means given the same arguments will return the same result come rain, come
107+
shine, sleet or snow. If the world is on fire and the socks have declared war on the bananas this function
108+
will behave exactly the same.
109+
110+
The more that you restrict the number of places where the current state of the system affects the behavoir of
111+
your code the easier it will be to maintain and extend your program over time.
112+
113+
Well I think that's it for now, that's a brief overview of functional programming next time we will
114+
cover the functional programmer's bread and butter, _maps_, _filters_ and _folds_.
115+
116+
117+
[cfm]: http://vknight.org/Computing_for_mathematics/
118+
[declarative-wiki]: https://en.wikipedia.org/wiki/Declarative_programming
119+
[functional-wiki]: https://en.wikipedia.org/wiki/Functional_programming
120+
[imperative-wiki]: https://en.wikipedia.org/wiki/Imperative_programming
121+
[oop-wiki]: https://en.wikipedia.org/wiki/Object-oriented_programming
122+
[paradigm-list]: https://en.wikipedia.org/wiki/Programming_paradigm

0 commit comments

Comments
 (0)