SlideShare a Scribd company logo
Name of the School: School of computer science and engineering
Course Code: OOP Course Name: E2UC201
Faculty Name: Rahul Anjana Programe Name:
Topic:
 FUNCTIONAL PROGRAMMING:
 First class,
 Higher order
 Proxy function
 Lambda function
Functional Programming
Functional Programming
Imperative programming is a paradigm in computer science that uses statements to
change a program's state. It's based on a statement-at-a-time paradigm, and the
order in which operations occur is crucial. Imperative programming languages
require an understanding of the functions necessary to solve a problem, rather than
a reliance on models that are able to solve it.
Example: C, Java, and Python.
Declarative programming is a coding style that tells a program what to do, not how
to do it. It uses a domain-specific language (DSL) that is usually closer to natural
language than pseudocode, making it more readable and easier to learn. The DSL
abstracts much of the boilerplate, leaving fewer lines of code to do the same work.
Example: HTML, SQL, CSS and XML. (eXtensible Markup Language)-
XML stands for Extensible Markup Language. It is a type of markup language and
file format used to store, transport, and recreate arbitrary data.CSS-Cascading Style
Sheets
Functional Programming
Pure and Impure Function:
 Functions that don’t modify their arguments or produce any other side-effects are called pure.
 Functions that modify their arguments or cause other actions to occur are called impure.
Functional Programming
#Example Impure:
def
remove_last(input_list):
input_list.pop()
names = [‘A', ‘B', ‘C']
remove_last(names)
print(names)
remove_last(names)
print(names)
#Example Pure:
def remove_last(input_list):
new_list = input_list.copy()
new_list.pop()
return new_list
names = ['A', 'B', 'C']
new_names = remove_last(names)
print(names)
print(new_names)
[‘A', ‘B']
[‘A']
[‘A', ‘B‘,’C’]
[‘A‘,’B’]
Functional Programming
Functional programming is a declarative programming paradigm style where one applies
pure functions in sequence to solve complex problems.
Functions take an input value and produce an output value without being affected by
the program.
Functional Programming Concepts
Functional Programming Concepts:
 First-class functions
 Recursion
 Immutability
 Pure functions
 High order functions
Functional Programming Concepts
1. Functions are objects: Python functions are first class objects. In the example below, we
are assigning function to a variable. This assignment doesn’t call the function. It takes the
function object referenced by shout and creates a second name pointing to it, yell.
Functional Programming Concepts
Functional Programming Concepts:
 Recursion: Functional programming languages rely on recursion rather than iteration.
Instead of iterating over a loop, a function in a functional programming language will call
itself.
# Solving for a factorial using recursion
def recursiveFactorial(n):
if (n == 0):
return 1;
# recursion call
return n *recursiveFactorial(n - 1);
print(recursiveFactorial(5))
Functional Programming Concepts
First-class functions First-class functions
In Python, the term “first-class function” refers to a function’s ability to be treated as an :
 object that can be assigned to a variable,
 used as an argument for other functions, and
 returned as a value.
As a result, functions in Python are identical to other objects like strings, integers, and lists.
Functional Programming Concepts
First-class functions First-class functions
 Function can be assigned to a variable:This allows for easy manipulation and reuse of
functions.
Example:
def square(x):
return x ** 2
my_func = square
print(my_func(3)) # Output: 9
Functional Programming Concepts
First-class functions First-class functions
 Functions can be passed as arguments to other functions: This is helpful for writing more
modular, reusable code as well as higher-order functions.
Exmple:
def apply_operation(func, x):
return func(x)
def square(x):
return x ** 2
print(apply_operation(square, 3)) # Output: 9
Functional Programming Concepts
First-class functions First-class functions
 Functions can also return values from other functions: This is useful for returning functions
based on specific criteria or for creating functions on the fly.
Example:
def get_operation(op):
if op == '+':
def add(x, y):
return x + y
return add
elif op == '-':
def subtract(x, y):
return x - y
return subtract
add_func = get_operation('+')
subtract_func = get_operation('-')
print(add_func(3, 4)) # Output: 7
print(subtract_func(10, 5)) # Output: 5
Functional Programming Concepts
Higher order functions
 A function that accepts other functions as parameters or returns functions as outputs is
called a high order function.
 This process applies a function to its parameters at each iteration while returning a new
function that accepts the next parameter.
 Common examples of higher-order functions include filter, map, and reduce.
 The idea of first-class functions in Python makes higher-order functions possible.
 Higher-order functions operate by accepting a function as an argument, altering it, and then
returning the altered function.
 More modular and reusable code can be produced as a result.
Built-in Higher-Order Functions in Python
Map, Filter, and Reduce
Functional Programming Concepts
Higher order functions
 Example
def apply_func(func, lst):
return [func(x) for x in lst]
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = apply_func(square,
numbers) print(squared_numbers)
# Output: [1, 4, 9, 16, 25]
Example 2
def make_adder(n):
def adder(x):
return x + n
return adder
add_five = make_adder(5)
print(add_five(10))
# Output: 15
Functional Programming Concepts
Proxy function
 The Proxy method is Structural design pattern that allows you to provide the replacement
for an another object.
 Here, we use different classes to represent the functionalities of another class.
 The most important part is that here we create an object having original object functionality
to provide to the outer world.
Functional Programming Concepts
Lambda function: What is a Lambda Function?
 Lambda functions are similar to user-defined functions but without a name.
 They're commonly referred to as anonymous functions.
 Lambda functions are efficient whenever you want to create a function that will only
contain simple expressions – that is, expressions that are usually a single line of a
statement.
 They're also useful when you want to use the function once.
Note: The anonymous function does not have a return keyword. This is because the
anonymous function will automatically return the result of the expression in the function once
it is executed.
Functional Programming Concepts
Lambda function: When Should You Use a Lambda Function?
You should use the lambda function to create simple expressions. For example,
expressions that do not include complex structures such as if-else, for-loops, and so
on.
So, for example, if you want to create a function with a for-loop, you should use a user-
defined function.
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
lambda argument(s) : expression
1.lambda is a keyword in Python for defining the anonymous function.
2.argument(s) is a placeholder, that is a variable that will be used to hold the value you
want to pass into the function expression. A lambda function can have multiple variables
depending on what you want to achieve.
3.expression is the code you want to execute in the lambda function.
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
lambda argument(s) : expression
Example:
(lambda x : x * 2)(3)
>> 6
def f(x):
return x * 2
print(f(3))
x=lambda x:x*3
print(x(2))
Functional Programming Concepts
Lambda function: How to Define a Lambda Function?
Immediately Invoked Function Expression
(lambda x, y: x + y)(2, 3)
(lambda x: x + 1)(2)
>>>high_ord_func = lambda x, func: x + func(x)
>>> high_ord_func(2, lambda x: x * x)
6
>>> high_ord_func(2, lambda x: x + 3)
7
Functional Programming Concepts
Examples:
(lambda x, y, z: x + y + z)(3, 8, 1)
12
print((lambda x: x if(x > 10) else 10)(5))
10
print((lambda x: x if(x > 10) else 10)(12))
12
Functional Programming Concepts
Examples:
(lambda x: x * 10 if x > 10 else (x * 5 if x < 5 else x))(11)
>>> high_ord_func = lambda x, func: x + func(x)
>>> high_ord_func(2, lambda x: x * x)
(lambda x, y, z=3: x + y + z)(1, 2)
(lambda x, y, z=3: x + y + z)(1, y=2)
(lambda *args: sum(args))(1,2,3)
y = 6
z = lambda x: x * y
print (z(8))
Functional Programming Concepts
Examples:
def myfunc(n):
return lambda a : a * n
Functional Programming Concepts
Examples:
min = (lambda x, y: x if x < y else y)
print(min(2, 3))
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
The functions map(), filter(), and reduce() all do the same thing: They
each take a function and a list of elements, and then return the result of
applying the function to each element in the list. As previously stated,
Python has built-in functions like map(), filter(), and reduce().
Functional Programming Concepts
we have three main functions:
•map()
•filter()
•reduce()
The map() function-The map() function or map and filter in Python (also
called as map filter in Python) is a higher-order function.
SYNTAX: map(function, iterables)
EXAMPLE-
def function(a):
return a*a
x = map(function, (1,2,3,4)) #x is the map object
print(set(x))
Functional Programming Concepts
x is a map object, as you can see. The map function is displayed next, which takes
“function()” as a parameter and then applies “a * a” to all ‘iterables’. As a result, all iterables’
values are multiplied by themselves before being returned.
The filter() function-
The filter() function is used to generate an output list of values that return true when the
function is called. It has the following syntax:
SYNTAX: filter (function, iterables)
This function like python map function map(), can take user-defined functions and lambda
functions as parameters.
EXAMPLE-
def func(x):
if x>=3:
return x
y = filter(func, (1,2,3,4))
print(y)
OUTPUT-[3, 4]
As you can see, y is the filter object, and the list is a collection of true values for the condition
(x>=3).
The reduce() function-
The reduce() function applies a provided function to ‘iterables’ and returns a single value, as the name
implies.
SYNTAX: reduce(function, iterables)
The function specifies which expression should be applied to the ‘iterables’ in this case. The function tools
module must be used to import this function.
EXAMPLE-
from functools import reduce
reduce(lambda a,b: a+b,[23,21,45,98])
OUTPUT-187
The reduce function in the preceding example adds each iterable in the list one by one and returns a single
result.
Ad

More Related Content

Similar to OOPS Object oriented Programming PPT Tutorial (20)

Functions in python, types of functions in python
Functions in python, types of functions in pythonFunctions in python, types of functions in python
Functions in python, types of functions in python
SherinRappai
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
AkhilTyagi42
 
Unit 2 - Functions in python - Prof Jishnu M S
Unit 2 - Functions in python - Prof Jishnu M SUnit 2 - Functions in python - Prof Jishnu M S
Unit 2 - Functions in python - Prof Jishnu M S
jishnums10
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
anaveenkumar4
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Matlab functions
Matlab functionsMatlab functions
Matlab functions
pramodkumar1804
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
kailashGusain3
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
RiteshKumarPradhan1
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
manikanta361
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
RohitYadav830391
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
Inzamam Baig
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
jigeno
 
Functions in python, types of functions in python
Functions in python, types of functions in pythonFunctions in python, types of functions in python
Functions in python, types of functions in python
SherinRappai
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
Unit 2 - Functions in python - Prof Jishnu M S
Unit 2 - Functions in python - Prof Jishnu M SUnit 2 - Functions in python - Prof Jishnu M S
Unit 2 - Functions in python - Prof Jishnu M S
jishnums10
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
anaveenkumar4
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
jigeno
 

Recently uploaded (20)

Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
BCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdfBCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdf
VENKATESHBHAT25
 
BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........
jinny kaur
 
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis""Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
Infopitaara
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
How to Make Material Space Qu___ (1).pptx
How to Make Material Space Qu___ (1).pptxHow to Make Material Space Qu___ (1).pptx
How to Make Material Space Qu___ (1).pptx
engaash9
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Elevate Your Workflow
Elevate Your WorkflowElevate Your Workflow
Elevate Your Workflow
NickHuld
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Gas Power Plant for Power Generation System
Gas Power Plant for Power Generation SystemGas Power Plant for Power Generation System
Gas Power Plant for Power Generation System
JourneyWithMe1
 
Building Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdfBuilding Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdf
rabiaatif2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
BCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdfBCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdf
VENKATESHBHAT25
 
BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........
jinny kaur
 
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis""Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
Infopitaara
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
How to Make Material Space Qu___ (1).pptx
How to Make Material Space Qu___ (1).pptxHow to Make Material Space Qu___ (1).pptx
How to Make Material Space Qu___ (1).pptx
engaash9
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Elevate Your Workflow
Elevate Your WorkflowElevate Your Workflow
Elevate Your Workflow
NickHuld
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Gas Power Plant for Power Generation System
Gas Power Plant for Power Generation SystemGas Power Plant for Power Generation System
Gas Power Plant for Power Generation System
JourneyWithMe1
 
Building Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdfBuilding Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdf
rabiaatif2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Ad

OOPS Object oriented Programming PPT Tutorial

  • 1. Name of the School: School of computer science and engineering Course Code: OOP Course Name: E2UC201 Faculty Name: Rahul Anjana Programe Name: Topic:  FUNCTIONAL PROGRAMMING:  First class,  Higher order  Proxy function  Lambda function
  • 3. Functional Programming Imperative programming is a paradigm in computer science that uses statements to change a program's state. It's based on a statement-at-a-time paradigm, and the order in which operations occur is crucial. Imperative programming languages require an understanding of the functions necessary to solve a problem, rather than a reliance on models that are able to solve it. Example: C, Java, and Python. Declarative programming is a coding style that tells a program what to do, not how to do it. It uses a domain-specific language (DSL) that is usually closer to natural language than pseudocode, making it more readable and easier to learn. The DSL abstracts much of the boilerplate, leaving fewer lines of code to do the same work. Example: HTML, SQL, CSS and XML. (eXtensible Markup Language)- XML stands for Extensible Markup Language. It is a type of markup language and file format used to store, transport, and recreate arbitrary data.CSS-Cascading Style Sheets
  • 4. Functional Programming Pure and Impure Function:  Functions that don’t modify their arguments or produce any other side-effects are called pure.  Functions that modify their arguments or cause other actions to occur are called impure.
  • 5. Functional Programming #Example Impure: def remove_last(input_list): input_list.pop() names = [‘A', ‘B', ‘C'] remove_last(names) print(names) remove_last(names) print(names) #Example Pure: def remove_last(input_list): new_list = input_list.copy() new_list.pop() return new_list names = ['A', 'B', 'C'] new_names = remove_last(names) print(names) print(new_names) [‘A', ‘B'] [‘A'] [‘A', ‘B‘,’C’] [‘A‘,’B’]
  • 6. Functional Programming Functional programming is a declarative programming paradigm style where one applies pure functions in sequence to solve complex problems. Functions take an input value and produce an output value without being affected by the program.
  • 7. Functional Programming Concepts Functional Programming Concepts:  First-class functions  Recursion  Immutability  Pure functions  High order functions
  • 8. Functional Programming Concepts 1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
  • 9. Functional Programming Concepts Functional Programming Concepts:  Recursion: Functional programming languages rely on recursion rather than iteration. Instead of iterating over a loop, a function in a functional programming language will call itself. # Solving for a factorial using recursion def recursiveFactorial(n): if (n == 0): return 1; # recursion call return n *recursiveFactorial(n - 1); print(recursiveFactorial(5))
  • 10. Functional Programming Concepts First-class functions First-class functions In Python, the term “first-class function” refers to a function’s ability to be treated as an :  object that can be assigned to a variable,  used as an argument for other functions, and  returned as a value. As a result, functions in Python are identical to other objects like strings, integers, and lists.
  • 11. Functional Programming Concepts First-class functions First-class functions  Function can be assigned to a variable:This allows for easy manipulation and reuse of functions. Example: def square(x): return x ** 2 my_func = square print(my_func(3)) # Output: 9
  • 12. Functional Programming Concepts First-class functions First-class functions  Functions can be passed as arguments to other functions: This is helpful for writing more modular, reusable code as well as higher-order functions. Exmple: def apply_operation(func, x): return func(x) def square(x): return x ** 2 print(apply_operation(square, 3)) # Output: 9
  • 13. Functional Programming Concepts First-class functions First-class functions  Functions can also return values from other functions: This is useful for returning functions based on specific criteria or for creating functions on the fly. Example: def get_operation(op): if op == '+': def add(x, y): return x + y return add elif op == '-': def subtract(x, y): return x - y return subtract add_func = get_operation('+') subtract_func = get_operation('-') print(add_func(3, 4)) # Output: 7 print(subtract_func(10, 5)) # Output: 5
  • 14. Functional Programming Concepts Higher order functions  A function that accepts other functions as parameters or returns functions as outputs is called a high order function.  This process applies a function to its parameters at each iteration while returning a new function that accepts the next parameter.  Common examples of higher-order functions include filter, map, and reduce.  The idea of first-class functions in Python makes higher-order functions possible.  Higher-order functions operate by accepting a function as an argument, altering it, and then returning the altered function.  More modular and reusable code can be produced as a result. Built-in Higher-Order Functions in Python Map, Filter, and Reduce
  • 15. Functional Programming Concepts Higher order functions  Example def apply_func(func, lst): return [func(x) for x in lst] def square(x): return x ** 2 numbers = [1, 2, 3, 4, 5] squared_numbers = apply_func(square, numbers) print(squared_numbers) # Output: [1, 4, 9, 16, 25] Example 2 def make_adder(n): def adder(x): return x + n return adder add_five = make_adder(5) print(add_five(10)) # Output: 15
  • 16. Functional Programming Concepts Proxy function  The Proxy method is Structural design pattern that allows you to provide the replacement for an another object.  Here, we use different classes to represent the functionalities of another class.  The most important part is that here we create an object having original object functionality to provide to the outer world.
  • 17. Functional Programming Concepts Lambda function: What is a Lambda Function?  Lambda functions are similar to user-defined functions but without a name.  They're commonly referred to as anonymous functions.  Lambda functions are efficient whenever you want to create a function that will only contain simple expressions – that is, expressions that are usually a single line of a statement.  They're also useful when you want to use the function once. Note: The anonymous function does not have a return keyword. This is because the anonymous function will automatically return the result of the expression in the function once it is executed.
  • 18. Functional Programming Concepts Lambda function: When Should You Use a Lambda Function? You should use the lambda function to create simple expressions. For example, expressions that do not include complex structures such as if-else, for-loops, and so on. So, for example, if you want to create a function with a for-loop, you should use a user- defined function.
  • 19. Functional Programming Concepts Lambda function: How to Define a Lambda Function? lambda argument(s) : expression 1.lambda is a keyword in Python for defining the anonymous function. 2.argument(s) is a placeholder, that is a variable that will be used to hold the value you want to pass into the function expression. A lambda function can have multiple variables depending on what you want to achieve. 3.expression is the code you want to execute in the lambda function.
  • 20. Functional Programming Concepts Lambda function: How to Define a Lambda Function? lambda argument(s) : expression Example: (lambda x : x * 2)(3) >> 6 def f(x): return x * 2 print(f(3)) x=lambda x:x*3 print(x(2))
  • 21. Functional Programming Concepts Lambda function: How to Define a Lambda Function? Immediately Invoked Function Expression (lambda x, y: x + y)(2, 3) (lambda x: x + 1)(2) >>>high_ord_func = lambda x, func: x + func(x) >>> high_ord_func(2, lambda x: x * x) 6 >>> high_ord_func(2, lambda x: x + 3) 7
  • 22. Functional Programming Concepts Examples: (lambda x, y, z: x + y + z)(3, 8, 1) 12 print((lambda x: x if(x > 10) else 10)(5)) 10 print((lambda x: x if(x > 10) else 10)(12)) 12
  • 23. Functional Programming Concepts Examples: (lambda x: x * 10 if x > 10 else (x * 5 if x < 5 else x))(11) >>> high_ord_func = lambda x, func: x + func(x) >>> high_ord_func(2, lambda x: x * x) (lambda x, y, z=3: x + y + z)(1, 2) (lambda x, y, z=3: x + y + z)(1, y=2) (lambda *args: sum(args))(1,2,3) y = 6 z = lambda x: x * y print (z(8))
  • 24. Functional Programming Concepts Examples: def myfunc(n): return lambda a : a * n
  • 25. Functional Programming Concepts Examples: min = (lambda x, y: x if x < y else y) print(min(2, 3)) def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11))
  • 26. The functions map(), filter(), and reduce() all do the same thing: They each take a function and a list of elements, and then return the result of applying the function to each element in the list. As previously stated, Python has built-in functions like map(), filter(), and reduce().
  • 27. Functional Programming Concepts we have three main functions: •map() •filter() •reduce() The map() function-The map() function or map and filter in Python (also called as map filter in Python) is a higher-order function. SYNTAX: map(function, iterables) EXAMPLE- def function(a): return a*a x = map(function, (1,2,3,4)) #x is the map object print(set(x))
  • 28. Functional Programming Concepts x is a map object, as you can see. The map function is displayed next, which takes “function()” as a parameter and then applies “a * a” to all ‘iterables’. As a result, all iterables’ values are multiplied by themselves before being returned. The filter() function- The filter() function is used to generate an output list of values that return true when the function is called. It has the following syntax: SYNTAX: filter (function, iterables) This function like python map function map(), can take user-defined functions and lambda functions as parameters. EXAMPLE- def func(x): if x>=3: return x y = filter(func, (1,2,3,4)) print(y)
  • 29. OUTPUT-[3, 4] As you can see, y is the filter object, and the list is a collection of true values for the condition (x>=3). The reduce() function- The reduce() function applies a provided function to ‘iterables’ and returns a single value, as the name implies. SYNTAX: reduce(function, iterables) The function specifies which expression should be applied to the ‘iterables’ in this case. The function tools module must be used to import this function. EXAMPLE- from functools import reduce reduce(lambda a,b: a+b,[23,21,45,98]) OUTPUT-187
  • 30. The reduce function in the preceding example adds each iterable in the list one by one and returns a single result.