programming functional inside oop?

38
Functional Programming inside OOP? It’s possible with Python

Upload: others

Post on 02-Oct-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Functional inside OOP?

FunctionalProgramminginside OOP

Itrsquos possible with Python

gtgtgtwhoami()

Carlos Villavicencio Ecuadorian 983992 Currently Python amp TypeScript

Community leader

Martial arts 剣道居合道

Nature photography enthusiast

Cayambe Volcano 2021

po5i

gtgtgtwhy_functional_programming

Easier and efficient

Divide and conquer

Ease debugging

Makes code simpler and readable

Also easier to test

gtgtgthistory()

Functions were first-class objects from design

Users wanted more functional solutions

1994 map filter reduce and lambdas were included

In Python 22 lambdas have access to the outer scope

ldquoNot having the choice streamlines the thought processrdquo- Guido van Rossum

The fate of reduce() in Python 3000

httpspython-historyblogspotcom200904origins-of-pythons-functional-featureshtml

gtgtgthas_django_fp()

httpsgithubcomdjangodjangoblob46786b4193e04d398532bbfc3dcf63c03c1793cbdjangoformsformsetspyL201-L213 httpsgithubcomdjangodjangoblobca9872905559026af82000e46cde6f7dedc897b6djangoformsformsetspyL316-L328

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 2: Programming Functional inside OOP?

gtgtgtwhoami()

Carlos Villavicencio Ecuadorian 983992 Currently Python amp TypeScript

Community leader

Martial arts 剣道居合道

Nature photography enthusiast

Cayambe Volcano 2021

po5i

gtgtgtwhy_functional_programming

Easier and efficient

Divide and conquer

Ease debugging

Makes code simpler and readable

Also easier to test

gtgtgthistory()

Functions were first-class objects from design

Users wanted more functional solutions

1994 map filter reduce and lambdas were included

In Python 22 lambdas have access to the outer scope

ldquoNot having the choice streamlines the thought processrdquo- Guido van Rossum

The fate of reduce() in Python 3000

httpspython-historyblogspotcom200904origins-of-pythons-functional-featureshtml

gtgtgthas_django_fp()

httpsgithubcomdjangodjangoblob46786b4193e04d398532bbfc3dcf63c03c1793cbdjangoformsformsetspyL201-L213 httpsgithubcomdjangodjangoblobca9872905559026af82000e46cde6f7dedc897b6djangoformsformsetspyL316-L328

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 3: Programming Functional inside OOP?

gtgtgtwhy_functional_programming

Easier and efficient

Divide and conquer

Ease debugging

Makes code simpler and readable

Also easier to test

gtgtgthistory()

Functions were first-class objects from design

Users wanted more functional solutions

1994 map filter reduce and lambdas were included

In Python 22 lambdas have access to the outer scope

ldquoNot having the choice streamlines the thought processrdquo- Guido van Rossum

The fate of reduce() in Python 3000

httpspython-historyblogspotcom200904origins-of-pythons-functional-featureshtml

gtgtgthas_django_fp()

httpsgithubcomdjangodjangoblob46786b4193e04d398532bbfc3dcf63c03c1793cbdjangoformsformsetspyL201-L213 httpsgithubcomdjangodjangoblobca9872905559026af82000e46cde6f7dedc897b6djangoformsformsetspyL316-L328

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 4: Programming Functional inside OOP?

gtgtgthistory()

Functions were first-class objects from design

Users wanted more functional solutions

1994 map filter reduce and lambdas were included

In Python 22 lambdas have access to the outer scope

ldquoNot having the choice streamlines the thought processrdquo- Guido van Rossum

The fate of reduce() in Python 3000

httpspython-historyblogspotcom200904origins-of-pythons-functional-featureshtml

gtgtgthas_django_fp()

httpsgithubcomdjangodjangoblob46786b4193e04d398532bbfc3dcf63c03c1793cbdjangoformsformsetspyL201-L213 httpsgithubcomdjangodjangoblobca9872905559026af82000e46cde6f7dedc897b6djangoformsformsetspyL316-L328

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 5: Programming Functional inside OOP?

gtgtgthas_django_fp()

httpsgithubcomdjangodjangoblob46786b4193e04d398532bbfc3dcf63c03c1793cbdjangoformsformsetspyL201-L213 httpsgithubcomdjangodjangoblobca9872905559026af82000e46cde6f7dedc897b6djangoformsformsetspyL316-L328

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 6: Programming Functional inside OOP?

Immutability An immutable object is an object

whose state cannot be modified after

it is created

Booleans strings and integers are

immutable objects

List and dictionaries are mutable

objects

Thread safety

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 7: Programming Functional inside OOP?

def update_list(value list) -gt None value += [10]

gtgtgtimmutability

gtgtgt foo = [1 2 3]gtgtgt id(foo)4479599424gtgtgt update_list(foo)gtgtgt foo[1 2 3 10]gtgtgt id(foo)4479599424

def update_number(value int) -gt None value += 10

gtgtgt foo = 10gtgtgt update_number(foo)gtgtgt foo10129300

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 8: Programming Functional inside OOP?

def update_number(value int) -gt None print(value id(value)) value += 10 print(value id(value))

gtgtgtimmutability

gtgtgt foo = 10gtgtgt update_number(foo)10 447822088020 4478221200gtgtgt foo10

httpsmediumcommeghamohanmutable-and-immutable-side-of-python-c2145cf72747

984707

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 9: Programming Functional inside OOP?

Decorators They are functions which modify the

functionality of other functions

Higher order functions

Closures

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 10: Programming Functional inside OOP?

gtgtgtdecorators

def increment(x int) -gt int return x + 1

gtgtgt increment(2)3

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 11: Programming Functional inside OOP?

gtgtgtdecorators

def increment(x int) -gt int return x + 1

def double_increment(func Callable) -gt Callable def wrapper(x int) r = func(x) func is saved in __closure__ y = r 2 return y return wrapper

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 12: Programming Functional inside OOP?

gtgtgtdecorators

double_incrementdef increment(x int) -gt int return x + 1

gtgtgt increment(2)6gtgtgt increment__closure__[0]cell_contentsltfunction increment at 0x7eff362cf940gtgtgtgt increment__closure__[0]cell_contents(2)3

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 13: Programming Functional inside OOP?

They reduce the number of arguments

that any function takes

Makes functions easier to compose

with others

Partial application of functions

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 14: Programming Functional inside OOP?

gtgtgtpartial_application

def get_url(url str role str) -gt str pass

from functools import partial

get_admin_url = partial(get_url admin)

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 15: Programming Functional inside OOP?

gtgtgtpartial_application

import refrom functools import partial

email_match = partial(rematch r^(w||_|-)+[](w|_|-|)+[]w23$)

url_match = partial(rematch

r(i)b((https|wwwd03[]|[a-z0-9-]+[][a-z]24)([^s()ltgt]+|(([^s()ltgt

]+|(([^s()ltgt]+)))))+((([^s()ltgt]+|(([^s()ltgt]+))))|[^s`()[]ltgt

laquoraquoldquordquolsquorsquo])))

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 16: Programming Functional inside OOP?

Lazy Evaluation It holds the evaluation of an

expression until the value is finally

needed

Reduce the memory footprint

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 17: Programming Functional inside OOP?

gtgtgtlazy_evaluation

def generator() i = 1 while True yield i i += 1

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 18: Programming Functional inside OOP?

gtgtgtlazy_evaluation

with open(filename r) as f for line in f process(line)

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 19: Programming Functional inside OOP?

Type Annotations PEP 484

Available since Python 35

Reduce bugs at runtime

Improves readability

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 20: Programming Functional inside OOP?

gtgtgt__annotations__

Read tutorial at

stackbuilderscom

Watch my talk at

PyCon China 2020

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 21: Programming Functional inside OOP?

Structural Pattern Matching

PEP-634

Available since Python 310

It doesnrsquot work as C or JavaScript

Itrsquos a declarative approach

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 22: Programming Functional inside OOP?

gtgtgtstructural_pattern_matching

point is an (x y) tuple[int int]match point case (0 0) print(Origin) case (0 y) print(fY=y) case (x 0) print(fX=x) case (x y) if x == y guard print(fX=Y=x) case (x y) print(fX=x Y=y) case _ wildcard raise ValueError(Not a point)httpsdocspythonorg310whatsnew310htmlpep-634-structural-pattern-matching

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 23: Programming Functional inside OOP?

gtgtgtstructural_pattern_matching

test_variable is a tuple[str Any int]match test_variable case (warning code 40) print(A warning has been received) case (error code _) print(fAn error code occurred)

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 24: Programming Functional inside OOP?

Other Functional Programming Patterns

When Python doesnrsquot offer a way to

do it you can always implement it

Currying

Composition

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 25: Programming Functional inside OOP?

gtgtgtcurrying

If a function ƒn takes n arguments then you can turn that into a

function cn which takes one argument and returns a function cnminus1

that takes nminus1 arguments and has access to the argument that

was passed to cn (hence cnminus1 is a closure)

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 26: Programming Functional inside OOP?

gtgtgtcurrying

def f_5(a int b int c int d int e int) -gt int return a + b + c + d + e

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 27: Programming Functional inside OOP?

gtgtgtcurrying

def c_5(a int) -gt Callable def c_4(b int) -gt Callable def c_3(c int) -gt Callable def c_2(d int) -gt Callable def c_1(e int) int return f_5(a b c d e) return c_1 return c_2 return c_3 return c_4

Then f_5(1 2 3 4 5) == c_5(1)(2)(3)(4)(5)

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 28: Programming Functional inside OOP?

curry(num_args=5)def c_5(a int b int c int d int e int) -gt int a + b + c + d + e

gtgtgtcurrying

httpssagnibakgithubioblogpython-is-haskell-currying

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 29: Programming Functional inside OOP?

gtgtgtcomposition

cat env|grep DEBUG

ASSETS_DEBUG=True

SENTRY_DEBUG=False

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 30: Programming Functional inside OOP?

gtgtgtcomposition

sortByDateDescending = reverse sortByDate

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 31: Programming Functional inside OOP?

gtgtgtcomposition

def compose2(f g) return lambda x f(g(x))

httpsmathieularosecomfunction-composition-in-python

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 32: Programming Functional inside OOP?

import functools

def compose(functions) def compose2(f g) return lambda x f(g(x)) return functoolsreduce(compose2 functions lambda x x)

gtgtgtcomposition

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 33: Programming Functional inside OOP?

def td(val str) -gt str return flttdgtvallttdgt

def tr(val str) -gt str return flttrgtvallttrgt

def table(val str) -gt str return flttablegtvallttablegt

gtgtgt one_cell_table = compose(table tr td)gtgtgt one_cell_table(something)lttablegtlttrgtlttdgtsomethinglttdgtlttrgtlttablegt

gtgtgtcomposition

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 34: Programming Functional inside OOP?

Testing Everything we covered before makes

our tests easier

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 35: Programming Functional inside OOP?

gtgtgtimport unittest

ldquoCode that is hard to test is not good coderdquo- Joe Eames

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 36: Programming Functional inside OOP?

gtgtgtimport unittest

ldquoThe outcome of a function is dependent only on the input and nothing elserdquo- Unknown author

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 37: Programming Functional inside OOP?

gtgtgtimport unittest

ldquoOO makes code understandable by encapsulating moving partsFP makes code understandable by minimizing moving partsrdquo

- Michael Feathers

httpsdevtoleolanesemaking-unit-test-fun-again-with-functional-programming-4g8m

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i

Page 38: Programming Functional inside OOP?

Thank you for your attention 128522

Questions

Feedback

Suggestions

po5i