Unit 05
Unit 05
Programming and
GUI programming
UNIT 2
Functional programming is a
programming paradigm in which code is
structured primarily in the form of functions.
Introducti The origins of this programming style arise
on to from a branch of mathematics known
as lambda calculus, which is the study of
functional functions and their mathematical properties.
programm In functional programming, you create a
ing solution by structuring code mainly using
functions.
In functional programming, functions can be
passed in as arguments to other functions
and can be returned by other functions.
A mapping is a one to one transformation. Each element in
the source is converted into a new value. The new values are
gathered into a collection, leaving the original collection
map, filter, unchanged. For example, suppose that you begin with the
list [1, 2, 3, 4, 5] and map using the transformation x*2+1.
and The result would be the list [3, 5, 7, 9, 11]
A filtering is the process of testing each value in a list with a
reduce function, and retaining only those for which the function is
true. If you begin with the list [1, 2, 3, 4, 5] and filter with a
function that returns true on the odd values, the result would
be the list [1, 3, 5].
A reduction is the process of applying a binary function to
each member of a list in a cumulative fashion. If you begin
with the list [1, 2, 3, 4, 5] and reduce using the addition
operation, the result would be ((((1 + 2) + 3) + 4) + 5), or
15.
# Example of using map, filter, and reduce in Pyth
on
data = [1, 2, 3, 4, 5]
Notice how each step in the program is a transformation that acts on an entire
collection as a whole, without the need to write a loop. By eliminating explicit loops
the program is both shorter and easier to understand. This makes functions written
in a functional style much easier to debug and correct.
In object-oriented programming (OOP), you structure your
code based on the concept of objects. In commonly used
OOP languages (such as Java or C#),
we create a template for an object using the keyword class,
and we instantiate an object by using the keyword new.
These objects contain fields that store data and methods
Combining that manipulate data. In this style, an object can be passed
as an argument in the function of another object.
Functional It is possible to write programs entirely in a functional
and Object- fashion. However, it is more common to use functional
programming features, and combine them with other
Oriented techniques.
An individual function might be written in a functional
Programmin fashion, while the remainder of a program is imperative.
g object-oriented programming. It is common for a class to
include individual methods (functions) that are themselves
written in a functional fashion.
To become a skilled Python programmer you need to learn
both the mechanics of all these techniques, as well as
being able to identify those situations in which it is
appropriate to use one approach or another.