Lecture 14 - Functional Programming, Python Sorting Flashcards

1
Q

Define first-class object

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do we know about functions in python? (1)

A

They’re first-class objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the features of functional programming? (4)

A
  1. Functions are first-class objects
  2. objects are typically immutable
  3. a function’s return value depends only on its input parameters
  4. Avoiding iteration – use recursion – map/reduce over lists
  5. declarative style (what to do), rather than imperative (how to do it)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the advantages of functional programming? (4)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the syntax for generator comprehension?

A

Same as list comprehension but with parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe lazy vs eager evaluation. Which is used by most languages?

A
  • 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
  1. Most use eager evaluation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are you doing when you make a generator?

A

Make the function able to be used as an iterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when yield is executed? (4)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an infinite generator? Give an example of this.

A
  • An infinite generator function is one that simply never reaches a “return” statement
  • A simple one is itertools.count()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does map do? (2)

A
  • Applies a function (1st arg) to the iterable (2nd arg)
  • Returns a map object which is an iterator over the results
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the args of filter? What does it return?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  • What does the reduce function do?
  • What are the args
A
  • ● 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the different callables? (6)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is currying?

A

the process of converting a multi-argument function into a chain of single-argument function applications is called “currying”,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the idea of a python closure

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe chain() from itertools (1)

A

chain together multiple iterators

17
Q

What does tee from itertools do?

A

split an iterator into two lazily-evaluated copies

18
Q

What does cycle from itertools do?

A

– cycle(): endlessly cycle through the elements of an iterable

19
Q

What does dropwhile from itertools do?

A

skip elements of an iterable until the first time a predicate
becomes false

20
Q

What does groupby from itertools do?

A

create sub-iterators grouped by the value of a keygen function

21
Q

What does partial from functools do?

A

return an object that behaves like a partially-applied function
● similar to currying

22
Q

What does wraps from functools do?

A

used to create function decorators like @staticmethod

23
Q

How do you sort a list in python?

A

Has a .sort() method which sorts it in place.

24
Q

How do you sort an iterable in python (two different argument sets)

A

result = sorted(iterable)
result = sorted(iterable,key,reverse)
● ‘key’ is a function which returns a sorting key
● ‘reverse’ is a boolean; if True, sort in descending order

25
For closures, when does the variable binding happen?
When the closure is created
26
For closures, exactly which variables are remembered?
only local variables in the enclosing scope of the innner def are bound ## Footnote Global variables are not remembered.
27
What happens if you change the values of the bound variables in "outer" after "inner" finishes but before "outer" returns?
Doesn't effect the variables in the closure
28
What are some uses of closures? (3)
* "partial application",. * currying (a specific instance of partial application) * "monkey patching",
29
What is partial application?
fixing one or more function arguments so that the result can be called by someone expecting to invoke a function with fewer arguments
30
What is currying?
a specific instance of partial application, applied repeatedly to reduce the number of arguments one-by-one until the final result takes only one argument
31
What is monkey patching?
we bind a function pointer so that we can modify the pointer while still being able to call the original function