|
| 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: true |
| 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/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. |
| 54 | + |
| 55 | +Whereas the functional paradigm is part of a family of [declarative programming][declarative-wiki] |
| 56 | +paradgims, where instead of detailing all the steps you simply 'declare' what something is or how to |
| 57 | +transform it into another form. In the case of [functional programming][functional-wiki] this is done |
| 58 | +(suprisingly) using functions, for example we can rewrite the above as the following. |
| 59 | + |
| 60 | +{% highlight py3 %} |
| 61 | + |
| 62 | +from functools import reduce |
| 63 | + |
| 64 | +def f_average(nums): |
| 65 | + """ |
| 66 | + The same as the calc_averaage function, just in a functional style |
| 67 | + """ |
| 68 | + sum = reduce(lambda x, y: x + y, nums, 0) |
| 69 | + |
| 70 | + return list(sum/len(nums)) |
| 71 | +{% endhighlight %} |
| 72 | + |
| 73 | +> Python 2.7.x Differences |
| 74 | +> |
| 75 | +> The ```reduce``` function was only moved into ```functools``` in Python > 3.0 so |
| 76 | +> it doesn't need to be imported. Also the result doesn't require being cast to |
| 77 | +> a list. |
| 78 | +
|
| 79 | +Don't worry too much if you don't understand the code above we will get around to how exactly |
| 80 | +this works in later posts, the important point is to note that this style is slightly more |
| 81 | +abstract and we let python handle some more of the details for us. |
| 82 | + |
| 83 | +## Pure vs Impure Functions |
| 84 | + |
| 85 | +There are two types of functions in the world _pure_ and _impure_. Below are two functions - can |
| 86 | +you spot the pure function from the impure function? |
| 87 | + |
| 88 | +{% highlight py3 %} |
| 89 | + |
| 90 | +def f(x): |
| 91 | + return x + 1 |
| 92 | + |
| 93 | +def g(x): |
| 94 | + if isTuesday(): |
| 95 | + return x + 1 |
| 96 | + else: |
| 97 | + return x |
| 98 | +{% endhighlight %} |
| 99 | + |
| 100 | +The function ```g``` is the impure function since it depends on the state of the system it is run on. |
| 101 | +Depending on if it is Tuesday or not this function may or may not do something, one of the main features |
| 102 | +of the functional paradigm and the functional programming langauges (such as Haskell) is that |
| 103 | +they deal in pure functions as far as possible. |
| 104 | + |
| 105 | +The function ```f``` is pure, which means given the same arguments will return the same result come rain, come |
| 106 | +shine, sleet or snow. If the world is on fire and the socks have declared war on the bananas this function |
| 107 | +will behave exactly the same. |
| 108 | + |
| 109 | +The more that you restrict the number of places where the current state of the system affects the behavoir of |
| 110 | +your code the easier it will be to maintain and extend your program over time. |
| 111 | + |
| 112 | +Well I think that's it for now, that's a brief overview of functional programming next time we will |
| 113 | +take a look at _lambdas_ and _higher order functions_. |
| 114 | + |
| 115 | + |
| 116 | +[cfm]: http://vknight.org/Computing_for_mathematics/ |
| 117 | +[declarative-wiki]: https://en.wikipedia.org/wiki/Declarative_programming |
| 118 | +[functional-wiki]: https://en.wikipedia.org/wiki/Functional_programming |
| 119 | +[imperative-wiki]: https://en.wikipedia.org/wiki/Imperative_programming |
| 120 | +[oop-wiki]: https://en.wikipedia.org/wiki/Object-oriented_programming |
| 121 | +[paradigm-list]: https://en.wikipedia.org/wiki/Programming_paradigm |
0 commit comments