In this article, I will show how you can use the map and lambda function. We will be using a Fibonacci sequence to demonstrate this. The task we will be carrying out is to produce a Fibonacci sequence and then apply map to it in order to cube it while utilizing a lamda function. We will be introducing each of them in turn.
First, the Fibonacci sequence.
The Fibonacci sequence was discovered by an Italian mathematician, Leonardo Fibonacci. The sequence goes in the form 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... Take a good look at the sequence and you will notice something. You will notice that from the third term, you need to sum the previous two terms to get the third term. That is , which is 1, is the sum of and . The general formula for each nth term is: and the first two terms are given as and .
So, let’s generate some code that will give us the Fibonacci sequence.
In the Fibonacci(num) function, we used a recursive definition for the Fibonacci function. As you can see from the last return, we called the Fibonacci function twice, asking it to sum the return values of the two previous calls.
Here is the code:
Now, we will create a list of the sum of any number of Fibonacci functions. For example, if we want to generate a list of the first n Fibonacci function, we would use the following code:
You will notice from the code above that I created a for loop that calls the Fibonacci function for each number we want to return. If you print the list, you will get the value for each Fibonacci number and all arranged in the list. For example, if we wanted to create a list of first five Fibonacci numbers the list, n_list, when printed, gives [0. 1. 1. 2. 3].
Now that we have our Fibonacci list ready. What we do next is use the map function to cube each element in the list. To carry out this cubing, I will use a lambda function.
But first, what is a map function?
A map function applies a given function to each element of an iterable such as a list, set, tuple etc. The syntax for the map function is map(function, iterable, …). This shows that the map can take more than one iterable. For example, let the function be a square function. We want to apply the square function to each element of the list [1,2,3,4].
This is how we would do it. First we write the square function and then use it as the function parameter in map, then insert the iterable as the second parameter in map. Here is the code:
The output from the above is:
[1, 4, 9, 16]
All the numbers squared. Used on its own, the map function will give you a map object. So, in order to print it out, I had to cast the map object to a list using list(map(square, the_list))
. You could even cast it to a set or any iterable if you want. Note that this map function is not used in our task. It is just an illustration. The one we use will come below.
Now that we have our map function set out, what about the lambda function.
The lambda function definition
A lambda function is a simple function. It is a function having no name. You know that normally you would define functions with a name, but for lambda functions you just want to return a value without defining a function. A lambda function can take any number of arguments but it has only one line or expression that it returns. I love lambda functions whenever I want to write a one-liner function. The square function above could have been done using a lambda function.
The syntax for a lambda function is: lambda x, y : expression
, where x and y are parameters. The expression is any valid operation on the parameters. You use only one line for it.
So, let’s go back to our task for today. We want to take the Fibonacci numbers list and cube every item in the list. We have already created the Fibonacci numbers list above. Now, how do we cube it combining a map and lambda function.
I will use just this one-liner.
From the lambda function above you can see that I used x as the parameter and the expression was that each x should be raised to power 3 or cubed. So, what the map function and lambda function is saying in essence is that take each item in n_list and then raise it to be power of 3 (or cube it) with map making sure we walk through each element in n_list. Map produces a map object and this object is then cast into a list using the list function and the value is referenced by cubed_fib which was then printed in the next line.
That is all for our task for today. If you want to keep on reading of other tasks I will be producing, just subscribe to this blog using the email subscription form above.
Here is the full code for the task:
For those of you who would like to have the file, you can download it here.
No comments:
Post a Comment
Your comments here!