What is "functional programming", "pure function", "functional language", "lambda function" and "higher-order function" and how these concepts work in modern programming?
Watch the video lesson from Svetlin Nakov and learn more at:
https://softuni.org/dev-concepts/what-is-functional-programming
The document defines and explains different types of functions in Python. It discusses defining functions, calling functions, passing arguments by reference versus value, writing functions using different approaches like anonymous functions and recursive functions. Some key points covered include: defining a function uses the def keyword followed by the function name and parameters; functions can be called by their name with arguments; arguments are passed by reference for mutable objects and by value for immutable objects; anonymous functions are defined using the lambda keyword and return a single expression; recursive functions call themselves to break down problems into sub-problems until a base case is reached.
following is work on Advance Python part 1 Functional Programming in Python
for code and more details plz do visit
https://lnkd.in/dnQF95z
for more free study material and Projects follow on
Github
https://lnkd.in/gYKtuB3
LinkedIn
https://lnkd.in/daSvf_P
#python #datascience #programming #machinelearning #github #deeplearning #coding #developer #projects #work #developers #linkedin #google #amazonindia#IBM
The document discusses functions in Python. It defines a function as a block of code that performs a specific task and only runs when called. Functions can take parameters as input and return values. Some key points covered include:
- User-defined functions can be created in Python in addition to built-in functions.
- Functions make code reusable, readable, and modular. They allow for easier testing and maintenance of code.
- Variables can have local, global, or non-local scope depending on where they are used.
- Functions can take positional/required arguments, keyword arguments, default arguments, and variable length arguments.
- Objects passed to functions can be mutable like lists, causing pass by
Functions are blocks of code that perform tasks and are called when needed. User-defined functions in Python are created using the def keyword. Functions make code reusable, increase readability and modularity. Variables inside functions have local scope unless declared as global or nonlocal. Functions can take arguments and return values. Libraries contain many built-in functions for tasks like math operations and string manipulation.
This document discusses functions in Python. It begins by defining what a function is and provides examples of built-in functions and functions defined in modules. It then lists some advantages of using functions such as code reusability and readability. The document discusses the different types of functions - built-in functions, functions defined in modules, and user-defined functions. It provides examples of each type. The document also covers topics such as function parameters, return values, variable scope, lambda functions, and using functions from libraries.
04_python_functions.ppt You can define functions to provide the required func...anaveenkumar4
You can define functions to provide the required functionality. Here are simple
rules to define a function in Python.
❖ Function blocks begin with the keyword def followed by the function name
and parentheses ( ).
❖ Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these parentheses.
❖ The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
❖ The code block within every function starts with a colon : and is indented.
❖ The statement return [expression] exits a function, optionally passing back
an expression to the caller. A return statement with no arguments is the
same as return None.
The document provides information on Python functions including defining, calling, passing arguments to, and scoping of functions. Some key points covered:
- Functions allow for modular and reusable code. User-defined functions in Python are defined using the def keyword.
- Functions can take arguments, have docstrings, and use return statements. Arguments are passed by reference in Python.
- Functions can be called by name and arguments passed positionally or by keyword. Default and variable arguments are also supported.
- Anonymous lambda functions can take arguments and return an expression.
- Variables in a function have local scope while global variables defined outside a function can be accessed anywhere. The global keyword is used to modify global variables from within a function
The document provides information on Python functions including defining, calling, passing arguments to, and scoping of functions. Some key points covered:
- Functions allow for modular and reusable code. User-defined functions in Python are defined using the def keyword.
- Functions can take arguments, have docstrings, and use return statements. Arguments are passed by reference in Python.
- Functions can be called by name and arguments passed positionally or by keyword. Default and variable arguments are also supported.
- Anonymous lambda functions can take arguments and return an expression.
- Variables in a function have local scope while global variables defined outside a function can be accessed anywhere. The global keyword is used to modify global variables from within a function
The document discusses different types of functions in MATLAB:
1) Functions allow grouping code to perform tasks and operate in their own workspace separately from the base workspace. They can accept multiple inputs and outputs.
2) Anonymous functions can be defined inline without a file using the @ syntax.
3) Primary functions must be in a file but can call sub-functions defined there as well.
4) Nested functions are defined within another function and share its workspace. Private functions reside in a private subfolder and are only visible locally.
5) Global variables can be shared between functions by declaring them globally at the start of relevant files.
This document provides an overview of a lecture on functional programming in Scala. It covers the following topics:
1. A recap of functional programming principles like functions as first-class values and no side effects.
2. An introduction to the Haskell programming language including its syntax for defining functions.
3. How functions are defined in Scala and how they are objects at runtime.
4. Examples of defining the factorial function recursively in Haskell and Scala, and making it tail recursive.
5. Concepts of first-class functions, currying, partial application, and an example of implementing looping in Scala using these techniques.
This document discusses functions in Python. It begins by defining what a function is - a block of code that performs a specific task and only runs when called. User-defined functions can be created in Python. The document outlines the advantages of using functions such as code reusability and readability. It provides an example of defining and calling a simple function. It also discusses variable scope in functions, including local, global, and nonlocal variables. Finally, it covers passing different data types like numbers, lists, dictionaries and strings to functions.
This document discusses functions in Python. It begins by defining what a function is - a block of code that performs a specific task and only runs when called. User-defined functions can be created in Python. The document outlines the advantages of using functions such as code reusability and readability. It provides an example of defining and calling a simple function. It discusses variable scope within functions and different types of arguments that can be passed to functions. The document also covers passing different data types like lists, dictionaries and strings to functions. Finally, it discusses using functions from library modules like math and string functions.
This document provides an introduction and overview of R programming for statistics. It discusses how to run R sessions and functions, basic math operations and data types in R like vectors, data frames, and matrices. It also covers statistical and graphical features of R, programming features like functions, and gives examples of built-in and user-defined functions.
This document discusses functions in Python. It defines a function as a block of code that performs a specific task and can be called when needed. Functions make programs easier to develop, test and reuse code. The document covers creating and calling user-defined functions, variable scope, passing arguments and return values, lambda functions, mutable and immutable objects, and built-in functions for common tasks like math operations and string manipulation.
This document discusses functions in Python. It defines a function as a named sequence of statements that performs a computation. Functions allow code to be reused by calling the function by name. Python has many built-in functions for tasks like type conversion and mathematics. Functions can take parameters and return values. Defining functions helps make programs easier to read, understand, debug and maintain.
Functions are reusable blocks of code that perform a specific task. They help reduce complexity, improve reusability and maintainability of code. There are built-in functions predefined in modules and user-defined functions. Built-in functions include type conversion, math operations etc. User-defined functions are created using the def keyword and defined with a name, parameters and indented block of code. Functions are called by their name with actual parameters. This transfers program control to the function block, executes code, then returns control to calling block.
Functions allow programmers to organize code into reusable blocks. There are built-in functions and user-defined functions. Functions make code easier to develop, test and reuse. Variables inside functions can be local, global or nonlocal. Parameters pass data into functions, while functions can return values. Libraries contain pre-defined functions for tasks like mathematics and string manipulation.
This document provides an overview of functional programming languages. It discusses key concepts like mathematical functions, lambda expressions, higher-order functions, and referential transparency. Specific functional languages covered include Lisp, Scheme, ML, Haskell, and how each handles concepts like recursion, lists, and lazy evaluation. Haskell is discussed in depth through examples of functions, list comprehensions, and its ability to handle infinite lists through lazy evaluation. The document concludes by comparing the characteristics of functional and imperative languages.
Value Stream Mapping Worskshops for Intelligent Continuous SecurityMarc Hornbeek
This presentation provides detailed guidance and tools for conducting Current State and Future State Value Stream Mapping workshops for Intelligent Continuous Security.
The document discusses functions in Python. It defines a function as a block of code that performs a specific task and only runs when called. Functions can take parameters as input and return values. Some key points covered include:
- User-defined functions can be created in Python in addition to built-in functions.
- Functions make code reusable, readable, and modular. They allow for easier testing and maintenance of code.
- Variables can have local, global, or non-local scope depending on where they are used.
- Functions can take positional/required arguments, keyword arguments, default arguments, and variable length arguments.
- Objects passed to functions can be mutable like lists, causing pass by
Functions are blocks of code that perform tasks and are called when needed. User-defined functions in Python are created using the def keyword. Functions make code reusable, increase readability and modularity. Variables inside functions have local scope unless declared as global or nonlocal. Functions can take arguments and return values. Libraries contain many built-in functions for tasks like math operations and string manipulation.
This document discusses functions in Python. It begins by defining what a function is and provides examples of built-in functions and functions defined in modules. It then lists some advantages of using functions such as code reusability and readability. The document discusses the different types of functions - built-in functions, functions defined in modules, and user-defined functions. It provides examples of each type. The document also covers topics such as function parameters, return values, variable scope, lambda functions, and using functions from libraries.
04_python_functions.ppt You can define functions to provide the required func...anaveenkumar4
You can define functions to provide the required functionality. Here are simple
rules to define a function in Python.
❖ Function blocks begin with the keyword def followed by the function name
and parentheses ( ).
❖ Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these parentheses.
❖ The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
❖ The code block within every function starts with a colon : and is indented.
❖ The statement return [expression] exits a function, optionally passing back
an expression to the caller. A return statement with no arguments is the
same as return None.
The document provides information on Python functions including defining, calling, passing arguments to, and scoping of functions. Some key points covered:
- Functions allow for modular and reusable code. User-defined functions in Python are defined using the def keyword.
- Functions can take arguments, have docstrings, and use return statements. Arguments are passed by reference in Python.
- Functions can be called by name and arguments passed positionally or by keyword. Default and variable arguments are also supported.
- Anonymous lambda functions can take arguments and return an expression.
- Variables in a function have local scope while global variables defined outside a function can be accessed anywhere. The global keyword is used to modify global variables from within a function
The document provides information on Python functions including defining, calling, passing arguments to, and scoping of functions. Some key points covered:
- Functions allow for modular and reusable code. User-defined functions in Python are defined using the def keyword.
- Functions can take arguments, have docstrings, and use return statements. Arguments are passed by reference in Python.
- Functions can be called by name and arguments passed positionally or by keyword. Default and variable arguments are also supported.
- Anonymous lambda functions can take arguments and return an expression.
- Variables in a function have local scope while global variables defined outside a function can be accessed anywhere. The global keyword is used to modify global variables from within a function
The document discusses different types of functions in MATLAB:
1) Functions allow grouping code to perform tasks and operate in their own workspace separately from the base workspace. They can accept multiple inputs and outputs.
2) Anonymous functions can be defined inline without a file using the @ syntax.
3) Primary functions must be in a file but can call sub-functions defined there as well.
4) Nested functions are defined within another function and share its workspace. Private functions reside in a private subfolder and are only visible locally.
5) Global variables can be shared between functions by declaring them globally at the start of relevant files.
This document provides an overview of a lecture on functional programming in Scala. It covers the following topics:
1. A recap of functional programming principles like functions as first-class values and no side effects.
2. An introduction to the Haskell programming language including its syntax for defining functions.
3. How functions are defined in Scala and how they are objects at runtime.
4. Examples of defining the factorial function recursively in Haskell and Scala, and making it tail recursive.
5. Concepts of first-class functions, currying, partial application, and an example of implementing looping in Scala using these techniques.
This document discusses functions in Python. It begins by defining what a function is - a block of code that performs a specific task and only runs when called. User-defined functions can be created in Python. The document outlines the advantages of using functions such as code reusability and readability. It provides an example of defining and calling a simple function. It also discusses variable scope in functions, including local, global, and nonlocal variables. Finally, it covers passing different data types like numbers, lists, dictionaries and strings to functions.
This document discusses functions in Python. It begins by defining what a function is - a block of code that performs a specific task and only runs when called. User-defined functions can be created in Python. The document outlines the advantages of using functions such as code reusability and readability. It provides an example of defining and calling a simple function. It discusses variable scope within functions and different types of arguments that can be passed to functions. The document also covers passing different data types like lists, dictionaries and strings to functions. Finally, it discusses using functions from library modules like math and string functions.
This document provides an introduction and overview of R programming for statistics. It discusses how to run R sessions and functions, basic math operations and data types in R like vectors, data frames, and matrices. It also covers statistical and graphical features of R, programming features like functions, and gives examples of built-in and user-defined functions.
This document discusses functions in Python. It defines a function as a block of code that performs a specific task and can be called when needed. Functions make programs easier to develop, test and reuse code. The document covers creating and calling user-defined functions, variable scope, passing arguments and return values, lambda functions, mutable and immutable objects, and built-in functions for common tasks like math operations and string manipulation.
This document discusses functions in Python. It defines a function as a named sequence of statements that performs a computation. Functions allow code to be reused by calling the function by name. Python has many built-in functions for tasks like type conversion and mathematics. Functions can take parameters and return values. Defining functions helps make programs easier to read, understand, debug and maintain.
Functions are reusable blocks of code that perform a specific task. They help reduce complexity, improve reusability and maintainability of code. There are built-in functions predefined in modules and user-defined functions. Built-in functions include type conversion, math operations etc. User-defined functions are created using the def keyword and defined with a name, parameters and indented block of code. Functions are called by their name with actual parameters. This transfers program control to the function block, executes code, then returns control to calling block.
Functions allow programmers to organize code into reusable blocks. There are built-in functions and user-defined functions. Functions make code easier to develop, test and reuse. Variables inside functions can be local, global or nonlocal. Parameters pass data into functions, while functions can return values. Libraries contain pre-defined functions for tasks like mathematics and string manipulation.
This document provides an overview of functional programming languages. It discusses key concepts like mathematical functions, lambda expressions, higher-order functions, and referential transparency. Specific functional languages covered include Lisp, Scheme, ML, Haskell, and how each handles concepts like recursion, lists, and lazy evaluation. Haskell is discussed in depth through examples of functions, list comprehensions, and its ability to handle infinite lists through lazy evaluation. The document concludes by comparing the characteristics of functional and imperative languages.
Value Stream Mapping Worskshops for Intelligent Continuous SecurityMarc Hornbeek
This presentation provides detailed guidance and tools for conducting Current State and Future State Value Stream Mapping workshops for Intelligent Continuous Security.
The B.Tech in Computer Science and Engineering (CSE) at Lovely Professional University (LPU) is a four-year undergraduate program designed to equip students with strong theoretical and practical foundations in computing. The curriculum is industry-aligned and includes core subjects like programming, data structures, algorithms, operating systems, computer networks, databases, and software engineering. Students can also choose specializations such as Artificial Intelligence, Data Science, Cybersecurity, and Cloud Computing. LPU emphasizes hands-on learning through modern labs, live projects, and internships. The university has collaborations with tech giants like Google, Microsoft, and IBM, offering students excellent exposure and placement opportunities. With a vibrant campus life, international diversity, and a strong placement record, LPU's B.Tech CSE program prepares students to become future-ready professionals in the fast-evolving tech world.
"Heaters in Power Plants: Types, Functions, and Performance Analysis"Infopitaara
This presentation provides a detailed overview of heaters used in power plants, focusing mainly on feedwater heaters, their types, construction, and role in improving thermal efficiency. It explains the difference between open and closed feedwater heaters, highlights the importance of low-pressure and high-pressure heaters, and describes the orientation types—horizontal and vertical.
The PPT also covers major heater connections, the three critical heat transfer zones (desuperheating, condensing, and subcooling), and key performance indicators such as Terminal Temperature Difference (TTD) and Drain Cooler Approach (DCA). Additionally, it discusses common operational issues, monitoring parameters, and the arrangement of steam and drip flows.
Understanding and maintaining these heaters is crucial for ensuring optimum power plant performance, reducing fuel costs, and enhancing equipment life.
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYijscai
With the increased use of Artificial Intelligence (AI) in malware analysis there is also an increased need to
understand the decisions models make when identifying malicious artifacts. Explainable AI (XAI) becomes
the answer to interpreting the decision-making process that AI malware analysis models use to determine
malicious benign samples to gain trust that in a production environment, the system is able to catch
malware. With any cyber innovation brings a new set of challenges and literature soon came out about XAI
as a new attack vector. Adversarial XAI (AdvXAI) is a relatively new concept but with AI applications in
many sectors, it is crucial to quickly respond to the attack surface that it creates. This paper seeks to
conceptualize a theoretical framework focused on addressing AdvXAI in malware analysis in an effort to
balance explainability with security. Following this framework, designing a machine with an AI malware
detection and analysis model will ensure that it can effectively analyze malware, explain how it came to its
decision, and be built securely to avoid adversarial attacks and manipulations. The framework focuses on
choosing malware datasets to train the model, choosing the AI model, choosing an XAI technique,
implementing AdvXAI defensive measures, and continually evaluating the model. This framework will
significantly contribute to automated malware detection and XAI efforts allowing for secure systems that
are resilient to adversarial attacks.
its all about Artificial Intelligence(Ai) and Machine Learning and not on advanced level you can study before the exam or can check for some information on Ai for project
The Fluke 925 is a vane anemometer, a handheld device designed to measure wind speed, air flow (volume), and temperature. It features a separate sensor and display unit, allowing greater flexibility and ease of use in tight or hard-to-reach spaces. The Fluke 925 is particularly suitable for HVAC (heating, ventilation, and air conditioning) maintenance in both residential and commercial buildings, offering a durable and cost-effective solution for routine airflow diagnostics.
Building Security Systems in Architecture.pdfrabiaatif2
Building security systems are essential for protecting people, property, and assets within a structure. These systems include a range of technologies and strategies such as surveillance cameras (CCTV), access control systems, alarm systems, security lighting, and motion detectors. Modern security solutions often integrate smart technology, allowing remote monitoring and real-time alerts through mobile devices. Access control systems, like key cards or biometric scanners, ensure that only authorized individuals can enter certain areas, enhancing both safety and privacy. Alarm systems, whether triggered by unauthorized entry, fire, or environmental hazards, play a critical role in emergency response. Additionally, video surveillance acts as both a deterrent and a tool for investigating incidents. An effective building security system is carefully planned during the design phase, taking into account the building's size, purpose, and potential risks. Ultimately, robust security systems are vital for ensuring peace of mind, protecting lives, and preserving valuable assets.
☁️ GDG Cloud Munich: Build With AI Workshop - Introduction to Vertex AI! ☁️
Join us for an exciting #BuildWithAi workshop on the 28th of April, 2025 at the Google Office in Munich!
Dive into the world of AI with our "Introduction to Vertex AI" session, presented by Google Cloud expert Randy Gupta.
This paper proposes a shoulder inverse kinematics (IK) technique. Shoulder complex is comprised of the sternum, clavicle, ribs, scapula, humerus, and four joints.
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.
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.
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
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))
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.