Lesson 17. Functional Programing With map

Functional Programming

Functional programming gives me migraines. I made a legit run at it and I could not wrap my brain around it. OO 4 lyfe yo!

What am I talking about? Programming paradigms.

In this tutorial, I am teaching you to write “scripts”. The word scripts usually refers to a set of instructions written in a non-compiled programming language that execute one after another. Scripts are usually relatively small programs that automate relatively simple and repetitive task. A good example is bash scripts or PowerShell scripts that are created to do things like move files around a network on a regular schedule. These scripts are usually kept in a single file.

I will not be teaching you how to write full blow Python programs in this tutorial. Being able to write full blown programs requires more knowledge than I can drop in a medium this simple. Full blow programs are huge and require many files to work together properly. How you code these files is called a programming paradigm.

There are a handful of paradigms. My OO comment earlier was a reference to object-oriented programming which is the dominant paradigm in modern software development. In recent years, functional programming has started to become more popular.

Functional programming consists of writing software in such a manner that functions are first class citizens. Everything is a function. If you spend your life in OO, moving to functions is a real mind bender. However, it does come in handy sometimes.

Sometimes you need to run the same operation over numerous data points. There are various ways to do that, but the most efficient way is to borrow some concepts from functional programing.

Functional programming is supported in Python. If you move into data science or learn to use Apache Spark, you are going to need a function called map.

Lambda Functions

The functions that we wrote in lesson 14 all had names. There is another way to write functions and that is anonymously.

Anonymous functions do not have names. The come in handy because they really reduce the code you have to write when the task is simple. You can fit an anonymous function on a single line.

Anonymous functions are implanted by using the lambda construct instead of def like with named functions.

map

Now that I have given you all that exposition, I can finally talk about map.

The map function lets you apply a function over an iterable object like a list. The map function takes a function as an argument and the iterable that the function is to be applied to.

Examples

Example #1 This Example Has No Name

Below is an example of an anonymous function. First, we review example 3 of lesson 14. Then we re-engineer it as an anonymous function. As you can see, the same thing can be achieved with much less code.

#original function
def divide(numerator, denominator):
    quotient = numerator / denominator
    return int(quotient)

print(divide(10,5))
#SHAZAM!
divide = lambda x,y: int(x / y)

print(divide(10,5))

Example #2 A Different Kind Of Loop

This example does not show the real power of map. We will not see that until we start using Python for data analysis. For now, here is a different way to iterate over a list of values.

The map function simply returns an object so before we can print it to the screen, we have to turn it into a list.

count = [1,2,3,4,5]

newcount = list(map(lambda x: x + 1, count))

print(newcount)

Now you try it!

Don't copy and past. Type the code yourself!

Last updated