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.

OOPS Object oriented Programming PPT Tutorial

  • 1.
    Name of theSchool: 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
  • 2.
  • 3.
    Functional Programming Imperative programmingis 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 andImpure 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 programmingis 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 FunctionalProgramming 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 FunctionalProgramming 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-classfunctions 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-classfunctions 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-classfunctions 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-classfunctions 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 Higherorder 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 Higherorder 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 Proxyfunction  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 Lambdafunction: 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 Lambdafunction: 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 Lambdafunction: 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 Lambdafunction: 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 Lambdafunction: 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: (lambdax, 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: (lambdax: 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: defmyfunc(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 wehave 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 xis 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 youcan 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 functionin the preceding example adds each iterable in the list one by one and returns a single result.