0% found this document useful (0 votes)
9 views34 pages

Lambda Functio

Research about an engineer

Uploaded by

shivam9031krs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

Lambda Functio

Research about an engineer

Uploaded by

shivam9031krs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

lambda function map filter reduce by src7cse @gmail.

com 30-Jul-2024 Pg 1 of 34

Lamda function, anonymous single line


functions in Python

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 1 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 2 of 34

homepage.cs.uiowa.edu/~sriram/16/spring13/notes/March15-2013.pdf

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 2 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 3 of 34

www.cs.cornell.edu/courses/cs3110/2010su/Lectures/AdamByrtekFPinPython.pdf

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 3 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 4 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 4 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 5 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 5 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 6 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 6 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 7 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 7 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 8 of 34

Benjamin Baka Python Data Structures and Algorithms Copyright © 2017 Packt Publishing pg-21

Benjamin Baka Python Data Structures and Algorithms Copyright © 2017 Packt Publishing pg-22

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 8 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 9 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 9 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 10 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 10 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 11 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 11 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 12 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 12 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 13 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 13 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 14 of 34

Lambda Expressions and Anonymous Functions

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 14 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 15 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 15 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 16 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 16 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 17 of 34

efaidnbmnnnibpcajpcglclefindmkaj/https://cs.brown.edu/courses/cs004/lectures/lec09.pdf

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 17 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 18 of 34

www.cse.iitd.ac.in/~pkalra/col100/slides/16-HOF.pdf

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 18 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 19 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 19 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 20 of 34

courses.cs.washington.edu/courses/cse143/12sp/explore/python3.pdf

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 20 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 21 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 21 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 22 of 34

Simply put, a lambda function is just like any normal python function, except
that it has no name when defining it, and it is contained in one line of code.

lambda argument(s): expression

A lambda function evaluates an expression for a given argument. You give the
function a value (argument) and then provide the operation (expression). The
keyword lambda must come first. A full colon (:) separates the argument and
the expression.
In the example code below, x is the argument and x+x is the expression.

#Normal python function


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 22 of 34
lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 23 of 34

def a_name(x):
return x+x

#Lambda function
lambda x: x+x

Before we get into practical applications, let’s mention some technicalities on


what the python community thinks is good and bad with lambda functions.
Pros
● Good for simple logical operations that are easy to understand. This
makes the code more readable too.
● Good when you want a function that you will use just one time.
Cons
● They can only perform one expression. It’s not possible to have
multiple independent operations in one lambda function.
● Bad for operations that would span more than one line in a normal
def function (For example nested conditional operations). If you need
a minute or two to understand the code, use a named function instead.
● Bad because you can’t write a doc-string to explain all the inputs,
operations, and outputs as you would in a normal def function.
But first, let’s look at situations when to use lambda functions. Note that we use
lambda functions a lot with python classes that take in a function as an
argument, for example, map() and filter(). These are also called Higher-order
functions.

1. Scalar values
This is when you execute a lambda function on a single value.
(lambda x: x*2)(12)
###Results
24
In the code above, the function was created and then immediately executed. This
is an example of an immediately invoked function expression.

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 23 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 24 of 34

2. Lists
filter(). This is a Python inbuilt library that returns only those values that fit
certain criteria. The syntax is filter(function, iterable). The iterable
can be any sequence such as a list, set, or series object (more below).
The example below filters a list for even numbers. Note that the filter function
returns a ‘Filter object’ and you need to encapsulate it with a list to return the
values.
list_1 = [1,2,3,4,5,6,7,8,9]
filter(lambda x: x%2==0, list_1)
### Results
<filter at 0xf378982348>
list(filter(lambda x: x%2==0, list_1))
###Results
[2, 4, 6, 8]

map(). This is another inbuilt python library with the syntax map(function,
iterable).
This returns a modified list where every value in the original list has been
changed based on a function. The example below cubes every number in the list.
list_1 = [1,2,3,4,5,6,7,8,9]
cubed = map(lambda x: pow(x,3), list_1)
list(cubed)
###Results
[1, 8, 27, 64, 125, 216, 343, 512, 729]

Lambdas are important for map(), filter() and reduce() because the arguments
that we pass to them are often short functions needing to be used only once in
our programs, so there’s no use in saving them.

Unlike regular functions that need to be defined and saved in memory,


anonymous functions are succinct or short and disposable.

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 24 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 25 of 34

Functional Programming

Functional Programming is a coding style that focuses on defining


what to do, instead of performing some action. Functional programming is
derived from the mathematical style of thinking where you define the kind of
inputs that go into a function and the kind of outputs that we can expect from the
function.
In functional code, the output of the function depends only on the arguments that
are passed. Calling the function f for the same value of x should return the same
result f(x) no matter how many times you pass it.
Thus, it calls for a radically different style of thinking where you are rarely
changing state. Instead of moving through steps, you think of data as
undergoing transformations with the desired result as the end state.
1. What are the characteristics of functional programming
2. How to achieve those characteristics.
3. What is the meaning of using functions as first class objects.
4. What is functional purity.
5. How to refactor procedural code to functional code.

Characteristics of functional programming


A functionally pure language should support the following constructs:
● Functions as first class objects, which means that you should be able to
apply all the constructs of using data, to functions as well.
● Pure functions; there should not be any side-effects in them
● Ways and constructs to limit the use of for loops
● Good support for recursion

Functions as first class objects in python:

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 25 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 26 of 34

Using functions as first class objects means to use them in the same manner
that you use data. So, You can pass them as parameters like passing a function
to another function as an argument.
For example, in the following example you can pass the int function as a
parameter to map function.

>>> list(map(int, ["1", "2", "3"]))


[1, 2, 3]

You can assign them to variables and return them. For example, in the following
code, you can assign the function hello_world, and then the variable will be
executed as a function.
Python functional purity:
There are various built-in functions in Python that can help to avoid procedural
code in functions. So something like this
def naive_sum(list):
s = 0
for l in list:
s += l
return s
can be replaced with the following construct:
sum(list)
Similarly, built-in functions such as map, reduce, and the itertools module in
Python can be utilized to avoid side-effects in your code.

Reducing the usage of loops in Python:


Loops come into the picture when you want to loop over a collection of objects
and apply some kind of logic or function.
for x in l:
func(x)

The above construct stems from the traditional thinking of visualizing the whole
program as a series of steps where you define how things need to be done.
lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 26 of 34
lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 27 of 34

Making this more functional will need a change in the thinking pattern. You can
replace the above for loop in Python in the following manner.
map(func, l)
This is read as “map the function to the list,” which conforms to our idea of
defining the question “what.”
If you take this idea and apply it to the sequential execution of functions, you get
the following construct.
def func1():
pass

def func2():
pass

def func3():
pass

executing = lambda f: f()


map(executing, [func1, func2, func3])

Please note that this does not actually run the functions but returns a lazy map
object. You need to pass this object to a list or any other eager function to have
the code executed.
Python Recursion:
What is Recursion
Recursion is a method of breaking a problem into subproblems which are
essentially of the same type as the original problem. You solve the base
problems and then combine the results. Usually this involves the function calling
itself.
An example for recursion may be something like:
eat the dumplings: 1. check how many dumplings on the plate 2. if no dumplings
left stop eating 3. else eat one dumpling 4. "eat the dumplings"
How to implement recursion in your code
Python functions support recursion and hence you can utilize the dynamic
programming constructs in the code to optimize them. Recursion basically needs
lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 27 of 34
lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 28 of 34

to fulfill two conditions. There should be a condition where the recursion should
end, and it should call itself for all other conditions. The end condition should be
limiting; i.e, the functions should call smaller versions of themselves.
Example: The following code generates Fibonacci numbers through recursion.

def fib(n):
if n == 0: return 0
elif n == 1: return 1
else: return fib(n-1)+fib(n-2)

A small example showing how to convert procedural code into functional code:
Let us go through a small example where you try to refactor procedural code into
functional. In the below example, you have a starting number which gets
squared, the result is incremented by 1 and the result of that is raised to the
power of 3, the code then takes the decrement. Note that the code works in
steps.

# procedural code
starting_number = 96

# get the square of the number


square = starting_number ** 2

# increment the number by 1


increment = square + 1

# cube of the number


cube = increment ** 3

# decrease the cube by 1


decrement = cube - 1

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 28 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 29 of 34

# get the final result


result = print(decrement) # output 783012621312

The same procedural code can be written functionally. Note that unlike the code
above instead of giving explicit instructions on how to do it, you are giving
instructions on what to do. The functions below operate at a higher plane of
abstraction.

# define a function `call` where you provide the function


and the arguments
def call(x, f):
return f(x)

# define a function that returns the square


square = lambda x : x*x

# define a function that returns the increment


increment = lambda x : x+1

# define a function that returns the cube


cube = lambda x : x*x*x

# define a function that returns the decrement


decrement = lambda x : x-1

# put all the functions in a list in the order that you


want to execute them
funcs = [square, increment, cube, decrement]

# bring it all together. Below is the non functional part.

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 29 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 30 of 34

# in functional programming you separate the functional and


the non functional parts.
from functools import reduce # reduce is in the functools
library
print(reduce(call, funcs, 96)) # output 783012621312

https://www.hackerearth.com/practice/python/functional-programming/functional-
programming-1/tutorial/

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 30 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 31 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 31 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 32 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 32 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 33 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 33 of 34


lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 34 of 34

lambda function map filter reduce by src7cse @gmail.com 30-Jul-2024 Pg 34 of 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy