Lecture 14 - Functional Programming, Python Sorting Flashcards
Define first-class object
An entity within a programming language that can:
- Appear in an expression
- Be assigned to a variable
- Be used as an argument
- Be returned by a function call
What do we know about functions in python? (1)
They’re first-class objects
What are the features of functional programming? (4)
- Functions are first-class objects
- objects are typically immutable
- a function’s return value depends only on its input parameters
- Avoiding iteration – use recursion – map/reduce over lists
- declarative style (what to do), rather than imperative (how to do it)
What are the advantages of functional programming? (4)
Modularity
- FP forces use of small, well-defined chunks of code
Composability
- – FP relies on data flowing between functions, so hooking together functions is easy
Ease of testing and debugging
- – when functions don’t depend on global state, every function can be unittested
Formal provability
- lack of assignments and side effects makes it much easier to prove invariants
What’s the syntax for generator comprehension?
Same as list comprehension but with parentheses
Describe lazy vs eager evaluation. Which is used by most languages?
- Lazy
- Only evaluate an expression when its value is actually needed
- Allows potentially infinite data structures
- – contrast with “eager” or strict evaluation, which always evaluates an expression even if the value is never actually use
- Most use eager evaluation
What are you doing when you make a generator?
Make the function able to be used as an iterator
What happens when yield is executed? (4)
- any function with “yield” is automatically modified to return a generator object which executes the code
- each time yield is executed, its value is returned as the next value of the iterator;
- the function will then resume execution at the next statement which would have executed without the yield
- when the function returns, exception StopIteration is raised automatically
What is an infinite generator? Give an example of this.
- An infinite generator function is one that simply never reaches a “return” statement
- A simple one is itertools.count()
What does map do? (2)
- Applies a function (1st arg) to the iterable (2nd arg)
- Returns a map object which is an iterator over the results
What are the args of filter? What does it return?
- args
- 1st: function that accepts the values from the iterator of the 2nd arg and returns a boolean
- 2nd: iterable
- Returns the elements from the iterator for which the 1st arg function returned true
- What does the reduce function do?
- What are the args
- ● reduce is repeated application of a binary function to accumulate a result
- – reduce(f,[a,b,c,d]) == f(f(f(a,b),c),d)
- – reduce(f,[a,b,c,d],init) == f(f(f(f(init,a),b),c),d)
What are the different callables? (6)
- named functions created with def keyword
● anonymous functions created with lambda
● instances of classes with __call__() method
● closures
– “operations with data attached” (conceptually the inverse of classes)
● static methods of instances
● generator functions
What is currying?
the process of converting a multi-argument function into a chain of single-argument function applications is called “currying”,
Describe the idea of a python closure
- A closure is an inner function that remembers and has access to variables in the local scope in which it was created even after the outer function has finished executing”
- In other words: When you have a function def inside another function def, and the outer function def returns the inner function def, if the inner function def uses a value from the outer function’s scope, that value is bound at the time the closure was created (aka when the outer function was called)