KEMBAR78
Functional Programming in Python | PDF
Python
Functional Programming
Haim Michael
November 14th
, 2018
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
lifemichael
Part 1:
https://youtu.be/nhQc-o0dUcM
Part 2:
https://youtu.be/IGeuQ1UBg1c
© 1996-2018 All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More
than 18 years of Practical Experience.
lifemichael
© 1996-2018 All Rights Reserved.
Haim Michael Introduction
● Professional Certifications
Zend Certified Engineer in PHP
Certified Java Professional
Certified Java EE Web Component Developer
OMG Certified UML Professional
● MBA (cum laude) from Tel-Aviv University
Information Systems Management
lifemichael
© 2008 Haim Michael 20151026
Introduction
 The main programming languages paradigms include
the following.
Imperative Paradigm
The program includes statements executed one by one that change the
state.
Structured Paradigm
Based on Imperative. The code has a more logical structure. Avoiding the
goto statement.
© 2008 Haim Michael 20151026
Introduction
Procedural Paradigm
Based on Structured. The code is split into procedures been called in
order to have a shorted code to maintain.
Object Oriented Paradigm
During the execution of our code objects are created in order to represent
the things our code deals with. Objects are connected with each other.
Methods can be invoked on these objects.
© 2008 Haim Michael 20151026
Introduction
Event Driven Paradigm
The control flow is determined mainly by the events (e.g. mouse clicks,
loading of data ends).
Declarative Paradigm
The code defines what we want to get without getting into the logic of it nor
the details.
© 2008 Haim Michael 20151026
Functional Programming
 Functional programming is a programming paradigm
that emphasizes the use of expressions and their
evaluation and especially through the definition of
functions that are treated as expressions. In addition, it
avoids the complexity involved with state changes as
the one when objects and variables.
© 2008 Haim Michael 20151026
Functional Programming
 The use of functions as expressions enable us getting
more expressive code. In many cases we will exploit
the power of recursion in order to get expressive
succinct (expressed in few words) code.
 Python is not a pure functional programming language.
Nevertheless, it has more than a few functional
programming capabilities.
© 2008 Haim Michael 20151026
Recursive Function
def total(numbers):
if len(numbers) == 0:
return 0
else:
return numbers[0] + total(numbers[1:])
print(total([2,5,7]))
© 2008 Haim Michael 20151026
Recursive Function
© 2008 Haim Michael 20151026
Pure Functions
 When we define a function that always returns the same
value for the very same arguments, and it doesn't depend
on any hidden information or state and its evaluation of the
result does not cause any observable side effects nor
output then it is a pure function.
 Pure functions are usually simpler and much easier to test
and are very popular in Python programming.
© 2008 Haim Michael 20151026
Pure Functions
 In order to write a pure function we should make sure that we
write local only code. We should make sure we don't use neither
the global statement nor the nonlocal one.
 Writing a lambda expression as a pure function is the common
approach.
© 2008 Haim Michael 20151026
Lambda Expression
 Using lambda expressions we can define a recursive
function that feels much more as an expression than a
function we define using the def keyword.
total = lambda numbers: 0 if len(numbers)==0 else numbers[0] +
total(numbers[1:])
print(total([5,2,3,6]))
© 2008 Haim Michael 20151026
Lambda Expression
© 2008 Haim Michael 20151026
Higher Order Functions
 When the function we define receives another function (or
functions) as an argument(s) or when its returned value is
another function it will called an higher order function.
 We can use higher order functions for creating new
functions in our code.
© 2008 Haim Michael 20151026
Higher Order Functions
data = [(13225324,"daniel",54), (3452344,"ronen",92),
(98234234,"moshe",80), (65354435,"yael",70)]
beststudent = lambda dat: max(dat, key=lambda ob:ob[2])
print(beststudent(data))
© 2008 Haim Michael 20151026
Higher Order Functions
© 2008 Haim Michael 20151026
Immutable Data
 One of the key characteristics of functional programming is
using immutable objects and constants instead of
variables.
 One of the possible advantages for this approach is the
performance advantage. Functional programming hardly
uses stateful objects.
© 2008 Haim Michael 20151026
Lazy Evaluation
 One of the functional programming characteristics that
improves its performance is the deferred computation till it
is required, also known as lazy evaluation.
 The yield statement is one example for the lazy evaluation
we can find in Python.
© 2008 Haim Michael 20151026
Lazy Evaluation
def numbers():
for num in range(10):
print("num=",num)
yield num
for number in numbers():
print(number)
© 2008 Haim Michael 20151026
Lazy Evaluation
© 2008 Haim Michael 20151026
Recursion instead of Loop
 When writing pure functional code we will avoid using
loops. We will use recursive functions instead.
total = lambda num: 0 if num==0 else num + total(num-1)
print(total(4))
© 2008 Haim Michael 20151026
Recursion instead of Loop
© 2008 Haim Michael 20151026
Currying Functions
 Currying is the technique of breaking down the
evaluation of a function that takes multiple arguments
into evaluating a sequence of singe argument
functions.
© 2008 Haim Michael 20151026
Currying Functions
def f(age):
def f1(num):
if age<80:
return num+10
elif age>=80 and age<=100:
return num+5
return f1
temp = f(85)(60)
print(temp)
© 2008 Haim Michael 20151026
Questions & Answers
Haim Michael
0546655837
Haim.Michael@gmail.com
blog.lifemichael.com

Functional Programming in Python

  • 1.
    Python Functional Programming Haim Michael November14th , 2018 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael Part 1: https://youtu.be/nhQc-o0dUcM Part 2: https://youtu.be/IGeuQ1UBg1c
  • 2.
    © 1996-2018 AllRights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 18 years of Practical Experience. lifemichael
  • 3.
    © 1996-2018 AllRights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management lifemichael
  • 4.
    © 2008 HaimMichael 20151026 Introduction  The main programming languages paradigms include the following. Imperative Paradigm The program includes statements executed one by one that change the state. Structured Paradigm Based on Imperative. The code has a more logical structure. Avoiding the goto statement.
  • 5.
    © 2008 HaimMichael 20151026 Introduction Procedural Paradigm Based on Structured. The code is split into procedures been called in order to have a shorted code to maintain. Object Oriented Paradigm During the execution of our code objects are created in order to represent the things our code deals with. Objects are connected with each other. Methods can be invoked on these objects.
  • 6.
    © 2008 HaimMichael 20151026 Introduction Event Driven Paradigm The control flow is determined mainly by the events (e.g. mouse clicks, loading of data ends). Declarative Paradigm The code defines what we want to get without getting into the logic of it nor the details.
  • 7.
    © 2008 HaimMichael 20151026 Functional Programming  Functional programming is a programming paradigm that emphasizes the use of expressions and their evaluation and especially through the definition of functions that are treated as expressions. In addition, it avoids the complexity involved with state changes as the one when objects and variables.
  • 8.
    © 2008 HaimMichael 20151026 Functional Programming  The use of functions as expressions enable us getting more expressive code. In many cases we will exploit the power of recursion in order to get expressive succinct (expressed in few words) code.  Python is not a pure functional programming language. Nevertheless, it has more than a few functional programming capabilities.
  • 9.
    © 2008 HaimMichael 20151026 Recursive Function def total(numbers): if len(numbers) == 0: return 0 else: return numbers[0] + total(numbers[1:]) print(total([2,5,7]))
  • 10.
    © 2008 HaimMichael 20151026 Recursive Function
  • 11.
    © 2008 HaimMichael 20151026 Pure Functions  When we define a function that always returns the same value for the very same arguments, and it doesn't depend on any hidden information or state and its evaluation of the result does not cause any observable side effects nor output then it is a pure function.  Pure functions are usually simpler and much easier to test and are very popular in Python programming.
  • 12.
    © 2008 HaimMichael 20151026 Pure Functions  In order to write a pure function we should make sure that we write local only code. We should make sure we don't use neither the global statement nor the nonlocal one.  Writing a lambda expression as a pure function is the common approach.
  • 13.
    © 2008 HaimMichael 20151026 Lambda Expression  Using lambda expressions we can define a recursive function that feels much more as an expression than a function we define using the def keyword. total = lambda numbers: 0 if len(numbers)==0 else numbers[0] + total(numbers[1:]) print(total([5,2,3,6]))
  • 14.
    © 2008 HaimMichael 20151026 Lambda Expression
  • 15.
    © 2008 HaimMichael 20151026 Higher Order Functions  When the function we define receives another function (or functions) as an argument(s) or when its returned value is another function it will called an higher order function.  We can use higher order functions for creating new functions in our code.
  • 16.
    © 2008 HaimMichael 20151026 Higher Order Functions data = [(13225324,"daniel",54), (3452344,"ronen",92), (98234234,"moshe",80), (65354435,"yael",70)] beststudent = lambda dat: max(dat, key=lambda ob:ob[2]) print(beststudent(data))
  • 17.
    © 2008 HaimMichael 20151026 Higher Order Functions
  • 18.
    © 2008 HaimMichael 20151026 Immutable Data  One of the key characteristics of functional programming is using immutable objects and constants instead of variables.  One of the possible advantages for this approach is the performance advantage. Functional programming hardly uses stateful objects.
  • 19.
    © 2008 HaimMichael 20151026 Lazy Evaluation  One of the functional programming characteristics that improves its performance is the deferred computation till it is required, also known as lazy evaluation.  The yield statement is one example for the lazy evaluation we can find in Python.
  • 20.
    © 2008 HaimMichael 20151026 Lazy Evaluation def numbers(): for num in range(10): print("num=",num) yield num for number in numbers(): print(number)
  • 21.
    © 2008 HaimMichael 20151026 Lazy Evaluation
  • 22.
    © 2008 HaimMichael 20151026 Recursion instead of Loop  When writing pure functional code we will avoid using loops. We will use recursive functions instead. total = lambda num: 0 if num==0 else num + total(num-1) print(total(4))
  • 23.
    © 2008 HaimMichael 20151026 Recursion instead of Loop
  • 24.
    © 2008 HaimMichael 20151026 Currying Functions  Currying is the technique of breaking down the evaluation of a function that takes multiple arguments into evaluating a sequence of singe argument functions.
  • 25.
    © 2008 HaimMichael 20151026 Currying Functions def f(age): def f1(num): if age<80: return num+10 elif age>=80 and age<=100: return num+5 return f1 temp = f(85)(60) print(temp)
  • 26.
    © 2008 HaimMichael 20151026 Questions & Answers Haim Michael 0546655837 Haim.Michael@gmail.com blog.lifemichael.com