SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

Linkers
LinkersLinkers
Linkers
Tech_MX
 
Object-oriented Programming in Python
Object-oriented Programming in PythonObject-oriented Programming in Python
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Kengatharaiyer Sarveswaran
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 
Airbnb Price Estimation in Major US Metropolitan Areas
Airbnb Price Estimation in Major US Metropolitan AreasAirbnb Price Estimation in Major US Metropolitan Areas
Airbnb Price Estimation in Major US Metropolitan Areas
RavitejChilukuri1
 
Software cost estimation
Software cost estimationSoftware cost estimation
Software cost estimation
djview
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
Mahmoud Masih Tehrani
 
Unification and Lifting
Unification and LiftingUnification and Lifting
Unification and Lifting
Megha Sharma
 
OS file systems
OS file systemsOS file systems
OS file systems
anand hd
 
1.algorithms
1.algorithms1.algorithms
1.algorithms
Chandan Singh
 
Problem solving agents
Problem solving agentsProblem solving agents
Problem solving agents
Megha Sharma
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
Archana Gopinath
 
Software project management
Software project managementSoftware project management
Software project management
Indu Sharma Bhardwaj
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
SAMIR BHOGAYTA
 
php
phpphp
php
ajeetjhajharia
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
Amit Kumar Rathi
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
A. S. M. Shafi
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
TM - Techniques
TM - TechniquesTM - Techniques
TM - Techniques
Rajendran
 
Introduction to problem solving in C
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in C
Diwakar Pratap Singh 'Deva'
 

Similar to Functional Programming in Python (20)

Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
KarthickT28
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
Geison Goes
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
Edward D. Weinberger
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
Edward D. Weinberger
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
kimmidalboc0
 
JNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptxJNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Functional python
Functional pythonFunctional python
Functional python
Jesué Junior
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
Riccardo Terrell
 
Rethink programming: a functional approach
Rethink programming: a functional approachRethink programming: a functional approach
Rethink programming: a functional approach
Francesco Bruni
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
Konrad Szydlo
 
Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional Programming
Knoldus Inc.
 
Functional programming
Functional programmingFunctional programming
Functional programming
ijcd
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
An Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User GroupAn Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User Group
Andreas Pauley
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
Carlos V.
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
KarthickT28
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
Geison Goes
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
 
Functional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David MertzFunctional programming in Python 1st Edition David Mertz
Functional programming in Python 1st Edition David Mertz
kimmidalboc0
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
Riccardo Terrell
 
Rethink programming: a functional approach
Rethink programming: a functional approachRethink programming: a functional approach
Rethink programming: a functional approach
Francesco Bruni
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
Konrad Szydlo
 
Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional Programming
Knoldus Inc.
 
Functional programming
Functional programmingFunctional programming
Functional programming
ijcd
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
An Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User GroupAn Introduction to Functional Programming at the Jozi Java User Group
An Introduction to Functional Programming at the Jozi Java User Group
Andreas Pauley
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
Carlos V.
 
Ad

More from Haim Michael (20)

The Visitor Classic Design Pattern [Free Meetup]
The Visitor Classic Design Pattern [Free Meetup]The Visitor Classic Design Pattern [Free Meetup]
The Visitor Classic Design Pattern [Free Meetup]
Haim Michael
 
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Haim Michael
 
Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Haim Michael
 
Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Haim Michael
 
Beyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and KotlinBeyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and Kotlin
Haim Michael
 
JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]
Haim Michael
 
Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]
Haim Michael
 
The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]
Haim Michael
 
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
The Visitor Classic Design Pattern [Free Meetup]
The Visitor Classic Design Pattern [Free Meetup]The Visitor Classic Design Pattern [Free Meetup]
The Visitor Classic Design Pattern [Free Meetup]
Haim Michael
 
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Haim Michael
 
Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Haim Michael
 
Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Haim Michael
 
Beyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and KotlinBeyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and Kotlin
Haim Michael
 
JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]
Haim Michael
 
Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]
Haim Michael
 
The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]
Haim Michael
 
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Ad

Recently uploaded (20)

Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink TemplateeeeeeeeeeeeeeeeeeeeeeeeeeNeuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentricIntegration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Natan Silnitsky
 
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Prachi Desai
 
The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!
PhilMeredith3
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...
Rishab Acharya
 
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink TemplateeeeeeeeeeeeeeeeeeeeeeeeeeNeuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentricIntegration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Natan Silnitsky
 
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Why Indonesia’s $12.63B Alt-Lending Boom Needs Loan Servicing Automation & Re...
Prachi Desai
 
The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!
PhilMeredith3
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...
Rishab Acharya
 
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 

Functional Programming in Python

  • 1. 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
  • 2. © 1996-2018 All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 18 years of Practical Experience. lifemichael
  • 3. © 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
  • 4. © 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.
  • 5. © 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.
  • 6. © 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.
  • 7. © 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.
  • 8. © 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.
  • 9. © 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]))
  • 10. © 2008 Haim Michael 20151026 Recursive Function
  • 11. © 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.
  • 12. © 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.
  • 13. © 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]))
  • 14. © 2008 Haim Michael 20151026 Lambda Expression
  • 15. © 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.
  • 16. © 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))
  • 17. © 2008 Haim Michael 20151026 Higher Order Functions
  • 18. © 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.
  • 19. © 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.
  • 20. © 2008 Haim Michael 20151026 Lazy Evaluation def numbers(): for num in range(10): print("num=",num) yield num for number in numbers(): print(number)
  • 21. © 2008 Haim Michael 20151026 Lazy Evaluation
  • 22. © 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))
  • 23. © 2008 Haim Michael 20151026 Recursion instead of Loop
  • 24. © 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.
  • 25. © 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)
  • 26. © 2008 Haim Michael 20151026 Questions & Answers Haim Michael 0546655837 [email protected] blog.lifemichael.com