SlideShare a Scribd company logo
1 of 48
Download to read offline
Applying functional with Python
Is it possible?
About me
● Position: Data Engineer at 99
● Production experience: Python, Java, Scala, Go and NodeJS
● Looking at: Erlang and Elixir
● twitter.com/@jesuejunior
● github.com/jesuejunior
Agenda
● OOP vs FP (2min)
● About Functional (3min)
● Imperative vs Functional (8min)
● Functions(2min)
● High-Order Functions(10min)
● Lazy Evaluation(8min)
● What did we miss?(4min)
● Next steps?(1min)
OOP vs FP
Psychology
Object-Oriented Programming (Imperative style)
● Often matches with human style of thinking
● Humans automatically parse the world into objects
● Any well-defined something that has a set of properties
● Just like you mentally divide the world into objects
Object-Oriented Programming
Functional Programming
● Does not (necessarily) divide code into objects
● And can lead to unintuitive code
● In cases where this doesn't match our Intuition
Functional Programming
Functional Programming
About Functional
About Functional
● Avoid state
● Immutable data
● First-class functions
● Higher-order functions
● Pure functions
● Recursion, tail recursion
● Iterators, sequences, lazy evaluation, pattern matching, monads....
Imperative mode
vs
Functional Mode
Imperative Mode
expr, res = "28+32+++32++39", 0
for t in expr.split("+"):
if t != "":
res += int(t)
print(res)
Imperative Mode
"28+32+++32++39", 0
"28", 0
"32", 28
"", 60
"", 60
"32", 60
"", 92
"39", 92
131
Functional Mode
from operator import add
expr = "28+32+++32++39"
res = reduce(add, map(int, filter(bool, expr.split("+")))
print(res)
Functional Mode
"28+32+++32++39"
["28","32","","","32","","39"]
["28","32","32","39"]
[28,32,32,39]
131
MAP/REDUCE/FILTER
● Readability VS. conciseness
● Technical aspects VS. operation substance
● Code reusage ("pluggability")
Python Hints
Python Hints - operator
In [3]: import operator
#Equivalent to 5 + 3
In [4]: operator.add(5, 3)
Out[4]: 8
#Equivalent to 2 < 5
In [5]: operator.lt(2,5)
Out[5]: True
#Equivalent to [1,2,3][1]
In [6]: operator.itemgetter(1)([1,2,3])
Out[6]: 2
#Equivalent to 3 ** 3
In [7]: operator.pow(2,3)
Out[7]: 8
Can we avoid loops?
In [17]: name = None
In [18]: while name is None:
...: name = input()
...: if len(name) < 2:
...: name = None
...:
In [25]: def get_name():
...: name = input()
...: return name if len(name) >= 2 else get_Name()
...:
Recursion
Imperative
Tail Recursion
Elixir Python
So sorry...
defmodule Fib do
def fib(0) do 0 end
def fib(1) do 1 end
def fib(n) do
fib(n-1) + fib(n-2)
end
end
IO.puts Fib.fib(10)
Functions
First Class
def add(a,b):
return a + b
add = lambda a,b: a + b
def calculations(a, b):
def add():
return a + b
return a, b, add
High-Order Function
Function as argument
map(lambda x: x^2, [1,2,3,4,5])
Function return function as result
def fill(topic):
print("My baloon is " + topic)
def timer(fn):
def inner(*args, **kwargs):
t = time()
fn(*args, **kwargs)
print("took {time}".format(time=time()-t))
return inner
filler = timer(fill)
filler("FP with Python")
I've already saw this...
@timer
def fill(topic):
print("My baloon is " + topic)
fill("FP with Python")
Partial Function Application
“ The process of fixing a number of
arguments to a function, producing
another function of smaller arity ”
Partial Function Application
import functools
def funcs(a, b, c):
return a + b + c
fp = functools.partial(funcs, c=3)
print(fp(1,2))
Currying
“ The technique of transforming a function
that takes multiple arguments in such a
way that it can be called as a chain of
functions each with a single argument ”
Currying
Currying
def compose(*funcs):
"""
Return a new function s.t.
compose(f,g,...)(x) == f(g(...(x)))
"""
def inner(data, funcs=funcs):
result = data
for f in reversed(funcs):
result = f(result)
return result
return inner
# >>> times2 = lambda x: x*2
# >>> minus3 = lambda x: x-3
# >>> mod6 = lambda x: x%6
# >>> f = compose(mod6, times2, minus3)
# >>> all(f(i)==((i-3)*2)%6 for i in range(1000000))
# True
Currying ( Standard Library)
In [32]: from operator import itemgetter
In [33]: itemgetter(3)([1,2,3,4,5])
Out[33]: 4
In [34]: from operator import attrgetter as attr
In [35]: class Balloon:
...: def __init__(self, name):
...: self.name = "[name] " + name
...:
In [36]: azul = Balloon('Azul')
In [37]: attr('name')(azul)
Out[37]: '[name] Azul'
Lazy Evaluation
List Comprehension
In [43]: res = [x**2 for x in range(10)]
...: print(res)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
List Comprehension (TIME)
python -m cProfile -s cumtime not_lazy_ex.py
3 function calls in 13.734 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 11.936 11.936 13.734 13.734 partial_ex.py:33(<module>)
1 1.798 1.798 1.798 1.798 {range}
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
List Comprehension (GENEXP)
In [44]: res = (x**2 for x in range(10))
...: print(res)
<generator object <genexpr> at 0x10fd98e60>
In [45]: for i in res: print(i, end=" ")
0 1 4 9 16 25 36 49 64 81
List Comprehension (GENEXP)
python -m cProfile -s cumtime lazy_ex.py
3 function calls in 1.812 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.812 1.812 partial_ex.py:30(<module>)
1 1.812 1.812 1.812 1.812 {range}
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
Iterable
from collections.abc import Iterable
class Fibonacci(Iterable):
def __init__(self):
self.a, self.b = 0, 1
self.total = 0
def __iter__(self):
return self
def __next__(self):
self.a, self.b = self.b, self.a + self.b
self.total += self.a
return self.a
def running_sum(self):
return self.total
Iterable
>>> fib = Fibonacci()
>>> fib.running_sum()
0
>>> for _, i in zip(range(10), fib):
... print(i, end=" ")
...
1 1 2 3 5 8 13 21 34 55
>>> fib.running_sum()
143
>>> next(fib)
89
Python & FP
Python & FP
PRO
● Functions as first-­class citizens
● Lambda
● Standard library: map/filter/reduce, itertools, operator,
functools
● Generators can be used for lazy­ evaluation (in some
cases)
Python & FP
CON
● Impossible to separate pure / non­ pure
● Mutable variables
● Costly mem copy operations
● Imperative style for cycles
● No optimization for tail recursion
What did we miss?
What did we miss?
ALMOST EVERYTHING
● Errors handling without exceptions
● Pattern matching
● Message passing
● Functional data structures
● Custom data types
● Immutable and mutable
What is the next?
● multipledispatch - A relatively sane approach to multiple dispatch in Python.
○ https://github.com/mrocklin/multipledispatch
● pyrsistent - Contains a number of immutable collections.
○ https://github.com/tobgu/pyrsistent
● toolz - Provides a set of utility functions for iterators, functions and dictionaries.
○ https://github.com/pytoolz/toolz
● hypothesis - Is a library for creating unit tests for finding edge cases in your code you wouldn't have thought to look for.
○ https://github.com/HypothesisWorks/hypothesis-python
● more_itertools - Tries to collect useful compositions of iterators that neither itertools nor the recipes included in its docs address.
○ https://github.com/erikrose/more-itertools
We're hiring
Join our team
carreiras.99app.com
Functional python

More Related Content

What's hot

2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrineFrankie Dintino
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functionalHackraft
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageAnıl Sözeri
 
Fantom on the JVM Devoxx09 BOF
Fantom on the JVM Devoxx09 BOFFantom on the JVM Devoxx09 BOF
Fantom on the JVM Devoxx09 BOFDror Bereznitsky
 
API design: using type classes and dependent types
API design: using type classes and dependent typesAPI design: using type classes and dependent types
API design: using type classes and dependent typesbmlever
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分bob_is_strange
 
The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196Mahmoud Samir Fayed
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better JavaGarth Gilmour
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional ProgrammingFrancesco Bruni
 
GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoGoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoEleanor McHugh
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift OverviewKaz Yoshikawa
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnMoriyoshi Koizumi
 

What's hot (20)

Advance python
Advance pythonAdvance python
Advance python
 
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Fantom on the JVM Devoxx09 BOF
Fantom on the JVM Devoxx09 BOFFantom on the JVM Devoxx09 BOF
Fantom on the JVM Devoxx09 BOF
 
API design: using type classes and dependent types
API design: using type classes and dependent typesAPI design: using type classes and dependent types
API design: using type classes and dependent types
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
 
The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
 
Testing for share
Testing for share Testing for share
Testing for share
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoGoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google Go
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
 

Similar to Functional python

Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monadsrkaippully
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobikrmboya
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonAnoop Thomas Mathew
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechtureAnatoly Bubenkov
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014PyData
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
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 PythonCarlos V.
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonPython Ireland
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in rubyKoen Handekyn
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Iterator protocol
Iterator protocolIterator protocol
Iterator protocolAkshar Raaj
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsMichael Pirnat
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 

Similar to Functional python (20)

Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobi
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
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
 
Selection sort
Selection sortSelection sort
Selection sort
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in Python
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Talk Code
Talk CodeTalk Code
Talk Code
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Iterator protocol
Iterator protocolIterator protocol
Iterator protocol
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 

Recently uploaded

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingSelcen Ozturkcan
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Recently uploaded (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Functional python

  • 1. Applying functional with Python Is it possible?
  • 2. About me ● Position: Data Engineer at 99 ● Production experience: Python, Java, Scala, Go and NodeJS ● Looking at: Erlang and Elixir ● twitter.com/@jesuejunior ● github.com/jesuejunior
  • 3. Agenda ● OOP vs FP (2min) ● About Functional (3min) ● Imperative vs Functional (8min) ● Functions(2min) ● High-Order Functions(10min) ● Lazy Evaluation(8min) ● What did we miss?(4min) ● Next steps?(1min)
  • 5. Object-Oriented Programming (Imperative style) ● Often matches with human style of thinking ● Humans automatically parse the world into objects ● Any well-defined something that has a set of properties ● Just like you mentally divide the world into objects
  • 7. Functional Programming ● Does not (necessarily) divide code into objects ● And can lead to unintuitive code ● In cases where this doesn't match our Intuition
  • 11. About Functional ● Avoid state ● Immutable data ● First-class functions ● Higher-order functions ● Pure functions ● Recursion, tail recursion ● Iterators, sequences, lazy evaluation, pattern matching, monads....
  • 13. Imperative Mode expr, res = "28+32+++32++39", 0 for t in expr.split("+"): if t != "": res += int(t) print(res)
  • 14. Imperative Mode "28+32+++32++39", 0 "28", 0 "32", 28 "", 60 "", 60 "32", 60 "", 92 "39", 92 131
  • 15. Functional Mode from operator import add expr = "28+32+++32++39" res = reduce(add, map(int, filter(bool, expr.split("+"))) print(res)
  • 17. MAP/REDUCE/FILTER ● Readability VS. conciseness ● Technical aspects VS. operation substance ● Code reusage ("pluggability")
  • 19. Python Hints - operator In [3]: import operator #Equivalent to 5 + 3 In [4]: operator.add(5, 3) Out[4]: 8 #Equivalent to 2 < 5 In [5]: operator.lt(2,5) Out[5]: True #Equivalent to [1,2,3][1] In [6]: operator.itemgetter(1)([1,2,3]) Out[6]: 2 #Equivalent to 3 ** 3 In [7]: operator.pow(2,3) Out[7]: 8
  • 20. Can we avoid loops? In [17]: name = None In [18]: while name is None: ...: name = input() ...: if len(name) < 2: ...: name = None ...: In [25]: def get_name(): ...: name = input() ...: return name if len(name) >= 2 else get_Name() ...: Recursion Imperative
  • 21. Tail Recursion Elixir Python So sorry... defmodule Fib do def fib(0) do 0 end def fib(1) do 1 end def fib(n) do fib(n-1) + fib(n-2) end end IO.puts Fib.fib(10)
  • 23. First Class def add(a,b): return a + b add = lambda a,b: a + b def calculations(a, b): def add(): return a + b return a, b, add
  • 25. Function as argument map(lambda x: x^2, [1,2,3,4,5])
  • 26. Function return function as result def fill(topic): print("My baloon is " + topic) def timer(fn): def inner(*args, **kwargs): t = time() fn(*args, **kwargs) print("took {time}".format(time=time()-t)) return inner filler = timer(fill) filler("FP with Python")
  • 27. I've already saw this... @timer def fill(topic): print("My baloon is " + topic) fill("FP with Python")
  • 28. Partial Function Application “ The process of fixing a number of arguments to a function, producing another function of smaller arity ”
  • 29. Partial Function Application import functools def funcs(a, b, c): return a + b + c fp = functools.partial(funcs, c=3) print(fp(1,2))
  • 30. Currying “ The technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions each with a single argument ”
  • 32. Currying def compose(*funcs): """ Return a new function s.t. compose(f,g,...)(x) == f(g(...(x))) """ def inner(data, funcs=funcs): result = data for f in reversed(funcs): result = f(result) return result return inner # >>> times2 = lambda x: x*2 # >>> minus3 = lambda x: x-3 # >>> mod6 = lambda x: x%6 # >>> f = compose(mod6, times2, minus3) # >>> all(f(i)==((i-3)*2)%6 for i in range(1000000)) # True
  • 33. Currying ( Standard Library) In [32]: from operator import itemgetter In [33]: itemgetter(3)([1,2,3,4,5]) Out[33]: 4 In [34]: from operator import attrgetter as attr In [35]: class Balloon: ...: def __init__(self, name): ...: self.name = "[name] " + name ...: In [36]: azul = Balloon('Azul') In [37]: attr('name')(azul) Out[37]: '[name] Azul'
  • 35. List Comprehension In [43]: res = [x**2 for x in range(10)] ...: print(res) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  • 36. List Comprehension (TIME) python -m cProfile -s cumtime not_lazy_ex.py 3 function calls in 13.734 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 11.936 11.936 13.734 13.734 partial_ex.py:33(<module>) 1 1.798 1.798 1.798 1.798 {range} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
  • 37. List Comprehension (GENEXP) In [44]: res = (x**2 for x in range(10)) ...: print(res) <generator object <genexpr> at 0x10fd98e60> In [45]: for i in res: print(i, end=" ") 0 1 4 9 16 25 36 49 64 81
  • 38. List Comprehension (GENEXP) python -m cProfile -s cumtime lazy_ex.py 3 function calls in 1.812 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 1.812 1.812 partial_ex.py:30(<module>) 1 1.812 1.812 1.812 1.812 {range} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
  • 39. Iterable from collections.abc import Iterable class Fibonacci(Iterable): def __init__(self): self.a, self.b = 0, 1 self.total = 0 def __iter__(self): return self def __next__(self): self.a, self.b = self.b, self.a + self.b self.total += self.a return self.a def running_sum(self): return self.total
  • 40. Iterable >>> fib = Fibonacci() >>> fib.running_sum() 0 >>> for _, i in zip(range(10), fib): ... print(i, end=" ") ... 1 1 2 3 5 8 13 21 34 55 >>> fib.running_sum() 143 >>> next(fib) 89
  • 42. Python & FP PRO ● Functions as first-­class citizens ● Lambda ● Standard library: map/filter/reduce, itertools, operator, functools ● Generators can be used for lazy­ evaluation (in some cases)
  • 43. Python & FP CON ● Impossible to separate pure / non­ pure ● Mutable variables ● Costly mem copy operations ● Imperative style for cycles ● No optimization for tail recursion
  • 44. What did we miss?
  • 45. What did we miss? ALMOST EVERYTHING ● Errors handling without exceptions ● Pattern matching ● Message passing ● Functional data structures ● Custom data types ● Immutable and mutable
  • 46. What is the next? ● multipledispatch - A relatively sane approach to multiple dispatch in Python. ○ https://github.com/mrocklin/multipledispatch ● pyrsistent - Contains a number of immutable collections. ○ https://github.com/tobgu/pyrsistent ● toolz - Provides a set of utility functions for iterators, functions and dictionaries. ○ https://github.com/pytoolz/toolz ● hypothesis - Is a library for creating unit tests for finding edge cases in your code you wouldn't have thought to look for. ○ https://github.com/HypothesisWorks/hypothesis-python ● more_itertools - Tries to collect useful compositions of iterators that neither itertools nor the recipes included in its docs address. ○ https://github.com/erikrose/more-itertools
  • 47. We're hiring Join our team carreiras.99app.com