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

Advance python
Advance pythonAdvance python
Advance python
pulkit agrawal
 
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
Frankie Dintino
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
Hackraft
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
Kent Ohashi
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
Bhanwar Singh Meena
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
Commit University
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
Giuseppe Arici
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Anı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 BOF
Dror 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 types
bmlever
 
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 196
Mahmoud Samir Fayed
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
pramode_ce
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
Garth 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 Programming
Francesco 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 Go
Eleanor McHugh
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
Kaz 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 Autumn
Moriyoshi 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, monads
rkaippully
 
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 Python
Anoop Thomas Mathew
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
Lennart Regebro
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
Anatoly 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 2014
PyData
 
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 Python
Carlos V.
 
Selection sort
Selection sortSelection sort
Selection sort
Abdelrahman Saleh
 
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 Python
Python Ireland
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
faradjpour
 
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
Steffen 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 Minutes
Matt Harrison
 
Iterator protocol
Iterator protocolIterator protocol
Iterator protocol
Akshar 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) Things
Michael Pirnat
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
PranavSB
 
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
Qiangning 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

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

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