programming paradigms -...
TRANSCRIPT
![Page 1: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/1.jpg)
Programming ParadigmsProcedural
Sequence of instructions that inform the computer what to do with the program's input
ExamplesC
PascalUnix (sh)
Object-Oriented Deal with collections of objects which maintain internal
state and support methods that query or modify this internal state in some way.
ExamplesJava
Smalltalk
FunctionalDecomposes into a set of functions, each of which solely takes inputs and produces outputs with no internal state.
ExamplesHaskellOCaml
ML
Declarative Specification describes the problem to be solved, and
language implementation figures out the details
ExamplesSQL
Prolog
![Page 2: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/2.jpg)
Programming ParadigmsProcedural
Sequence of instructions that inform the computer what to do with the program's input
ExamplesC
PascalUnix (sh)
Object-Oriented Deal with collections of objects which maintain internal
state and support methods that query or modify this internal state in some way.
ExamplesJava
Smalltalk
FunctionalDecomposes into a set of functions, each of which solely takes inputs and produces outputs with no internal state.
ExamplesHaskellOCaml
ML
Declarative Specification describes the problem to be solved, and
language implementation figures out the details
ExamplesSQL
Prolog
Multi-Paradigm Supports several different
paradigms, to be combined freely
ExamplesScalaC++
Python
![Page 3: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/3.jpg)
Functional Programming Concepts
![Page 4: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/4.jpg)
Primary entity is a "function"
Functional Programming Concepts
![Page 5: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/5.jpg)
Primary entity is a "function""Pure" functions are mathematical
Functional Programming Concepts
![Page 6: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/6.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input
Functional Programming Concepts
![Page 7: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/7.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input No side effects that modify internal state
Functional Programming Concepts
![Page 8: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/8.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input No side effects that modify internal state print() and file.write() are side effects
Functional Programming Concepts
![Page 9: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/9.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input No side effects that modify internal state print() and file.write() are side effectsStrict (Haskell): no assignments, variables, or state
Functional Programming Concepts
![Page 10: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/10.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input No side effects that modify internal state print() and file.write() are side effectsStrict (Haskell): no assignments, variables, or stateFlexible (Python): encourage low-interference functions
Functional Programming Concepts
![Page 11: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/11.jpg)
Primary entity is a "function""Pure" functions are mathematical Output depends only on input No side effects that modify internal state print() and file.write() are side effectsStrict (Haskell): no assignments, variables, or stateFlexible (Python): encourage low-interference functions Functional-looking interface but use variables, state internally
Functional Programming Concepts
![Page 12: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/12.jpg)
Why Functional Programming?
![Page 13: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/13.jpg)
Why avoid objects and side effects?
Why Functional Programming?
![Page 14: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/14.jpg)
Why avoid objects and side effects?Formal Provability Line-by-line invariants
Why Functional Programming?
![Page 15: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/15.jpg)
Why avoid objects and side effects?Formal Provability Line-by-line invariantsModularity Encourages small independent functions
Why Functional Programming?
![Page 16: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/16.jpg)
Why avoid objects and side effects?Formal Provability Line-by-line invariantsModularity Encourages small independent functionsComposability Arrange existing functions for new goals
Why Functional Programming?
![Page 17: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/17.jpg)
Why avoid objects and side effects?Formal Provability Line-by-line invariantsModularity Encourages small independent functionsComposability Arrange existing functions for new goalsEasy Debugging Behavior depends only on input
Why Functional Programming?
![Page 18: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/18.jpg)
Why avoid objects and side effects?Formal Provability Line-by-line invariantsModularity Encourages small independent functionsComposability Arrange existing functions for new goalsEasy Debugging Behavior depends only on input
Let's Get Started!
Why Functional Programming?
![Page 19: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/19.jpg)
Map / Filter
![Page 20: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/20.jpg)
Common Pattern
![Page 21: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/21.jpg)
output = [] for element in iterable: val = function(element) output.append(val) return output
Common Pattern
![Page 22: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/22.jpg)
output = [] for element in iterable: val = function(element) output.append(val) return output
return [function(element) for element in iterable]
Common Pattern
![Page 23: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/23.jpg)
output = [] for element in iterable: val = function(element) output.append(val) return output
return [function(element) for element in iterable]
Common Pattern
![Page 24: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/24.jpg)
output = [] for element in iterable: val = function(element) output.append(val) return output
return [function(element) for element in iterable]
Common Pattern
![Page 25: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/25.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
len
![Page 26: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/26.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
6
len
![Page 27: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/27.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
6
len
![Page 28: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/28.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
46
len
![Page 29: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/29.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
46
len
![Page 30: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/30.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4
len
![Page 31: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/31.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4
len
![Page 32: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/32.jpg)
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4 3
len
![Page 33: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/33.jpg)
[ , , , ]
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4 3
len
![Page 34: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/34.jpg)
[ , , , ]
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4 3
lenApply some function to every element of a sequence
![Page 35: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/35.jpg)
map(fn, iter)
No discussion of elements!
map :: (a -> b) x [a] -> [b]
![Page 36: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/36.jpg)
< , , , >
[len(s) for s in languages]["python", "perl", "java", "c++"]
46 4 3
map(len, languages)
![Page 37: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/37.jpg)
Another Common Pattern
![Page 38: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/38.jpg)
output = [] for element in iterable: if predicate(element): output.append(val) return output
Another Common Pattern
![Page 39: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/39.jpg)
output = [] for element in iterable: if predicate(element): output.append(val) return output
[element for element in iterable if predicate(element)]
Another Common Pattern
![Page 40: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/40.jpg)
output = [] for element in iterable: if predicate(element): output.append(val) return output
[element for element in iterable if predicate(element)]
Another Common Pattern
![Page 41: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/41.jpg)
output = [] for element in iterable: if predicate(element): output.append(val) return output
[element for element in iterable if predicate(element)]
Another Common Pattern
![Page 42: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/42.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
is_even
![Page 43: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/43.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
is_even
![Page 44: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/44.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
is_even
![Page 45: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/45.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
2
is_even
![Page 46: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/46.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
2
is_even
![Page 47: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/47.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
2
is_even
![Page 48: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/48.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82
is_even
![Page 49: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/49.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82
is_even
![Page 50: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/50.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82
is_even
![Page 51: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/51.jpg)
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82 34
is_even
![Page 52: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/52.jpg)
[ , , ]
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82 34
is_even
![Page 53: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/53.jpg)
[ , , ]
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82 34
is_even Only keep elements that satisfy some predicate
![Page 54: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/54.jpg)
filter(pred, iter)
No discussion of elements!
filter :: (a -> bool) x [a] -> [a]
![Page 55: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/55.jpg)
< , , >
[num for num in fibs if is_even(num)]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
82 34
filter(is_even, fibs)
![Page 56: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/56.jpg)
Lambda Functions
![Page 57: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/57.jpg)
Anonymous, on-the-fly, unnamed functions
Lambda Functions
![Page 58: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/58.jpg)
lambda params: expr(params)Returns an expression
Keyword lambda creates an anonymous function
![Page 59: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/59.jpg)
Defined Functions vs. Lambdas
![Page 60: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/60.jpg)
def greet(): print("Hi!")
Defined Functions vs. Lambdasdef binds a function object
to a namefunction
bytecode
greet
greet
![Page 61: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/61.jpg)
def greet(): print("Hi!")
lambda val: val ** 2lambda x, y: x * ylambda pair: pair[0] * pair[1]
Defined Functions vs. Lambdasdef binds a function object
to a name
lambda only creates a function object
function
bytecode
<lambda>
function
bytecode
greet
greet
![Page 62: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/62.jpg)
def greet(): print("Hi!")
lambda val: val ** 2lambda x, y: x * ylambda pair: pair[0] * pair[1]
(lambda x: x > 3)(4) # => True
Defined Functions vs. Lambdasdef binds a function object
to a name
lambda only creates a function object
function
bytecode
<lambda>
function
bytecode
greet
greet
![Page 63: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/63.jpg)
Using Lambdas
![Page 64: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/64.jpg)
triple = lambda x: x * 3 # NEVER EVER DO THIS
Using Lambdas
![Page 65: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/65.jpg)
triple = lambda x: x * 3 # NEVER EVER DO THIS
# Squares from 0**2 to 9**2map(lambda val: val ** 2, range(10))
Using Lambdas
![Page 66: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/66.jpg)
triple = lambda x: x * 3 # NEVER EVER DO THIS
# Squares from 0**2 to 9**2map(lambda val: val ** 2, range(10))
# Tuples with positive second elementsfilter(lambda pair: pair[1] > 0, [(4,1), (3, -2), (8,0)]
Using Lambdas
![Page 67: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/67.jpg)
Iterators and Generators
![Page 68: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/68.jpg)
Represent data stream, returned one element at a time
Iterators
![Page 69: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/69.jpg)
Iterators
![Page 70: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/70.jpg)
Iterators are objects, like (almost) everything in PythonRepresent finite or infinite data streams
Iterators
![Page 71: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/71.jpg)
Iterators are objects, like (almost) everything in PythonRepresent finite or infinite data streams
Use next(iterator) to yield successive values Raises StopIteration error upon termination
Iterators
![Page 72: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/72.jpg)
Iterators are objects, like (almost) everything in PythonRepresent finite or infinite data streams
Use next(iterator) to yield successive values Raises StopIteration error upon terminationUse iter(data) to build an iterator for a data structure
Iterators
![Page 73: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/73.jpg)
Iterable
![Page 74: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/74.jpg)
# Build an iterator over [1,2,3]it = iter([1,2,3])
Iterable
![Page 75: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/75.jpg)
# Build an iterator over [1,2,3]it = iter([1,2,3])
next(it) # => 1
Iterable
![Page 76: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/76.jpg)
# Build an iterator over [1,2,3]it = iter([1,2,3])
next(it) # => 1next(it) # => 2
Iterable
![Page 77: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/77.jpg)
# Build an iterator over [1,2,3]it = iter([1,2,3])
next(it) # => 1next(it) # => 2next(it) # => 3
Iterable
![Page 78: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/78.jpg)
# Build an iterator over [1,2,3]it = iter([1,2,3])
next(it) # => 1next(it) # => 2next(it) # => 3next(it) # raises StopIteration error
Iterable
![Page 79: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/79.jpg)
For Loops use Iterators
![Page 80: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/80.jpg)
for data in data_source: process(data)
For Loops use Iterators
![Page 81: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/81.jpg)
for data in data_source: process(data)
# is reallyfor data in iter(data_source): process(data)
For Loops use Iterators
![Page 82: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/82.jpg)
for data in data_source: process(data)
# is reallyfor data in iter(data_source): process(data)
For Loops use Iterators
Iterator sees changes to the underlying data structure
![Page 83: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/83.jpg)
Builtins use Iterators
![Page 84: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/84.jpg)
# Return a valuemax(iterable) min(iterable)val in iterable val not in iterableall(iterable) any(iterable)
Builtins use Iterators
![Page 85: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/85.jpg)
# Return a valuemax(iterable) min(iterable)val in iterable val not in iterableall(iterable) any(iterable)
Builtins use IteratorsConsume iterable until return value is known
![Page 86: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/86.jpg)
# Return a valuemax(iterable) min(iterable)val in iterable val not in iterableall(iterable) any(iterable)
Builtins use IteratorsConsume iterable until return value is known
What happens for infinite iterators?
![Page 87: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/87.jpg)
# Return a valuemax(iterable) min(iterable)val in iterable val not in iterableall(iterable) any(iterable)
# Return an iteratorenumerate(iterable) zip(*iterables)map(fn, iterable) filter(pred, iterable)
Builtins use IteratorsConsume iterable until return value is known
What happens for infinite iterators?
![Page 88: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/88.jpg)
# Return a valuemax(iterable) min(iterable)val in iterable val not in iterableall(iterable) any(iterable)
# Return an iteratorenumerate(iterable) zip(*iterables)map(fn, iterable) filter(pred, iterable)
Builtins use IteratorsConsume iterable until return value is known
What happens for infinite iterators?
To convert to list, use list(iterable)
![Page 89: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/89.jpg)
Generators
![Page 90: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/90.jpg)
"Resumable Functions"
Generators
![Page 91: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/91.jpg)
Regular Functions vs. Generator Functions
![Page 92: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/92.jpg)
Regular Functions Generators
Regular Functions vs. Generator Functions
![Page 93: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/93.jpg)
Regular FunctionsReturn a single, computed value
GeneratorsReturn an iterator that generates a stream of values
Regular Functions vs. Generator Functions
![Page 94: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/94.jpg)
Regular FunctionsReturn a single, computed value
Each call generates a new private namespace and new local variables, then variables are thrown away
GeneratorsReturn an iterator that generates a stream of values
Local variables aren't thrown away when exiting a function - you can resume where you left off!
Regular Functions vs. Generator Functions
![Page 95: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/95.jpg)
Simple Generator
![Page 96: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/96.jpg)
def generate_ints(n): for i in range(n): yield i
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 97: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/97.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 98: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/98.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)type(g) # => <class 'generator'>
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 99: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/99.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)type(g) # => <class 'generator'>next(g) # => 0
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 100: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/100.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)type(g) # => <class 'generator'>next(g) # => 0next(g) # => 1
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 101: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/101.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)type(g) # => <class 'generator'>next(g) # => 0next(g) # => 1next(g) # => 2
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 102: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/102.jpg)
def generate_ints(n): for i in range(n): yield i
g = generate_ints(3)type(g) # => <class 'generator'>next(g) # => 0next(g) # => 1next(g) # => 2next(g) # raises StopIteration
Simple GeneratorThe yield keyword tells Python to
convert the function into a generator
![Page 103: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/103.jpg)
def generate_fibs(): a, b = 0, 1 while True: a, b = b, a + b yield a
Another Generator
Infinite data stream of Fibonacci numbers
![Page 104: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/104.jpg)
Using Our Generator
![Page 105: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/105.jpg)
g = generate_fibs()
Using Our Generator
![Page 106: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/106.jpg)
g = generate_fibs()
next(g) # => 1
Using Our Generator
![Page 107: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/107.jpg)
g = generate_fibs()
next(g) # => 1next(g) # => 1
Using Our Generator
![Page 108: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/108.jpg)
g = generate_fibs()
next(g) # => 1next(g) # => 1next(g) # => 2next(g) # => 3next(g) # => 5
Using Our Generator
![Page 109: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/109.jpg)
g = generate_fibs()
next(g) # => 1next(g) # => 1next(g) # => 2next(g) # => 3next(g) # => 5max(g) # Oh no! What happens?
Using Our Generator
![Page 110: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/110.jpg)
def fibs_under(n): for f in generate_fibs(): # Loops over 1, 1, 2, … if f > n: break print(f)
Lazy Generation
![Page 111: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/111.jpg)
Summary: Why Use Iterators and Generators?
![Page 112: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/112.jpg)
Compute data on demand Reduces in-memory buffering Can avoid expensive function calls
Summary: Why Use Iterators and Generators?
![Page 113: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/113.jpg)
Compute data on demand Reduces in-memory buffering Can avoid expensive function callsrange, map, filter and others are iterable
Summary: Why Use Iterators and Generators?
![Page 114: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/114.jpg)
Compute data on demand Reduces in-memory buffering Can avoid expensive function callsrange, map, filter and others are iterableGreat for asynchronous programming (network/web)
Summary: Why Use Iterators and Generators?
![Page 115: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/115.jpg)
Decorators
![Page 116: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/116.jpg)
Functions as Arguments
![Page 117: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/117.jpg)
# map(fn, iterable)# filter(pred, iterable)
Functions as Arguments
![Page 118: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/118.jpg)
# map(fn, iterable)# filter(pred, iterable)
def perform_twice(fn, *args, **kwargs): fn(*args, **kwargs) fn(*args, **kwargs)
Functions as Arguments
![Page 119: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/119.jpg)
# map(fn, iterable)# filter(pred, iterable)
def perform_twice(fn, *args, **kwargs): fn(*args, **kwargs) fn(*args, **kwargs)
perform_twice(print, 5, 10, sep='&', end='...')# => 5&10...5&10...
Functions as Arguments
![Page 120: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/120.jpg)
Functions as Return Values
![Page 121: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/121.jpg)
def make_divisibility_test(n): def divisible_by_n(m): return m % n == 0 return divisible_by_n
Functions as Return Values
![Page 122: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/122.jpg)
def make_divisibility_test(n): def divisible_by_n(m): return m % n == 0 return divisible_by_n
div_by_3 = make_divisibility_test(3)
Functions as Return Values
![Page 123: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/123.jpg)
def make_divisibility_test(n): def divisible_by_n(m): return m % n == 0 return divisible_by_n
div_by_3 = make_divisibility_test(3)filter(div_by_3, range(10)) # generates 0, 3, 6, 9
Functions as Return Values
![Page 124: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/124.jpg)
def make_divisibility_test(n): def divisible_by_n(m): return m % n == 0 return divisible_by_n
div_by_3 = make_divisibility_test(3)filter(div_by_3, range(10)) # generates 0, 3, 6, 9make_divisibility_test(5)(10) # => True
Functions as Return Values
![Page 125: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/125.jpg)
¿Por qué no los dos?
Functions as Argumentsf
x
f(x)
g
x'
g(x')
![Page 126: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/126.jpg)
¿Por qué no los dos?
Functions as Arguments Functions as Return Valuesf
x
f(x)
g
x'
g(x')
![Page 127: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/127.jpg)
¿Por qué no los dos?
Decorator!Functions as Arguments Functions as Return Values
f
x
f(x)
g
x'
g(x')
![Page 128: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/128.jpg)
Writing Our First Decorator
![Page 129: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/129.jpg)
def debug(function):
Writing Our First Decorator
![Page 130: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/130.jpg)
def debug(function): def wrapper(*args, **kwargs):
Writing Our First Decorator
![Page 131: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/131.jpg)
def debug(function): def wrapper(*args, **kwargs): print("Arguments:", args, kwargs)
Writing Our First Decorator
![Page 132: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/132.jpg)
def debug(function): def wrapper(*args, **kwargs): print("Arguments:", args, kwargs) return function(*args, **kwargs)
Writing Our First Decorator
![Page 133: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/133.jpg)
def debug(function): def wrapper(*args, **kwargs): print("Arguments:", args, kwargs) return function(*args, **kwargs) return wrapper
Writing Our First Decorator
![Page 134: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/134.jpg)
Using our debug decorator
![Page 135: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/135.jpg)
def foo(a, b, c=1): return (a + b) * c
Using our debug decorator
![Page 136: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/136.jpg)
def foo(a, b, c=1): return (a + b) * c
foo = debug(foo)
Using our debug decorator
![Page 137: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/137.jpg)
def foo(a, b, c=1): return (a + b) * c
foo = debug(foo)foo(2, 3) # prints "Arguments: (2, 3) {}# => returns 5
Using our debug decorator
![Page 138: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/138.jpg)
def foo(a, b, c=1): return (a + b) * c
foo = debug(foo)foo(2, 3) # prints "Arguments: (2, 3) {}# => returns 5foo(2, 1, c=3) # prints "Arguments: (2, 1) {'c': 3}"# => returns 9
Using our debug decorator
![Page 139: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/139.jpg)
def foo(a, b, c=1): return (a + b) * c
foo = debug(foo)foo(2, 3) # prints "Arguments: (2, 3) {}# => returns 5foo(2, 1, c=3) # prints "Arguments: (2, 1) {'c': 3}"# => returns 9print(foo) # <function debug.<locals>.wrapper at 0x...>
Using our debug decorator
![Page 140: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/140.jpg)
def foo(a, b, c=1): return (a + b) * c
foo = debug(foo)foo(2, 3) # prints "Arguments: (2, 3) {}# => returns 5foo(2, 1, c=3) # prints "Arguments: (2, 1) {'c': 3}"# => returns 9print(foo) # <function debug.<locals>.wrapper at 0x...>
Using our debug decorator
It seems like overkill to say foo twice here
![Page 141: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/141.jpg)
Using our debug decorator
![Page 142: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/142.jpg)
@debugdef foo(a, b, c=1): return (a + b) * c
Using our debug decorator
![Page 143: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/143.jpg)
@debugdef foo(a, b, c=1): return (a + b) * c
Using our debug decorator
![Page 144: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/144.jpg)
@debugdef foo(a, b, c=1): return (a + b) * c
@decorator applies a decorator to the following function
Using our debug decorator
![Page 145: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/145.jpg)
@debugdef foo(a, b, c=1): return (a + b) * c
foo(5, 3, c=2) # prints "Arguments: (5, 3) {'c': 2}"# => returns 16
@decorator applies a decorator to the following function
Using our debug decorator
![Page 146: Programming Paradigms - Personepages.di.unipi.it/corradini/Didattica/AP-17/SLIDES/PythonFP.pdfProgramming Paradigms Procedural Sequence of instructions that inform the computer what](https://reader035.vdocuments.mx/reader035/viewer/2022062414/5f01be287e708231d400d236/html5/thumbnails/146.jpg)
Python Documentation, of courseGuide to Functional ProgrammingA few other sites, which I've unfortunately forgotten.
Credit