Module 2: Functional programming II Flashcards

Decorators, Currying, Partial, Closures

1
Q

What are useful applications of higher-order functions?

A
  • Function composition
  • Decorators
  • Currying
  • Partial
  • Closures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Decorators?

A

Decorators take an existing function and add some functionality (or “decoration”) over it

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

What is this code exemplifying?
def make_polite(func):
def inner():
print(“Hello, world! “,)
func()
return inner

@make_polite
def another_function():
print(“I am some other ordinary
function.”)

A

Decorator
The code is an example of a DECORATOR because it wraps another function (another_function) to modify its behavior without changing its code. The make_polite function adds a greeting before the original function’s output. Using @make_polite applies this modification, demonstrating a decorator’s purpose: to extend or alter a function’s behavior dynamically.

When you apply @make_polite to another_function, another_function gets wrapped by inner. Therefore, calling another_function() now actually calls inner(), which in turn does two things:

Prints “Hello, world!”
Calls the original another_function() content, printing “I am some other ordinary function.”

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

Does a decorator modify the internal code of a function?

A

No, decorators add functionality but DON”T change the internal code of a functions thanks to function composition.
new function = decorator (original_function)

Original function is not changed and we keep the same interface.

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

Why are decorators useful?

A

Useful to maintain and augment existing code that is already working!

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

What are the phases when using decorators? Explain.

A
  1. Wrap: at definition time, the decorator takes the original function and returns
    a new wrapped function
  2. Evaluate: at evaluation time, the wrapping function usually calls the original
    function. The wrapping function can pre-process the argument values or
    post-process the return value (or both)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some common scenarios in which decorators are useful?

A
  • Logging
  • Security
  • Handling incomplete data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How could you rewrite this multi-argument function?
f(a,b,c) = x

A

h = g(a)
i = h(b)
j = i(c) = x

or more compactly:
g(a)(b)(c)

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

What is currying?

A

The process of turning multi-argument functions into a composition of single argument functions

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

What is the natural way to implement currying?

A

Using decorators to add curry to the function

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

What does currying allow us to do?

A
  • Look at the intermediate steps of a function
  • Create specialized functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does currying rely on?

A

Function composition

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

Why is currying interesting from a functional programming perspective?

A
  • by separating one complex multi-argument function into single argument functions,
    we have simpler components: functions only have one input and one output
  • functions are compatible with theoretical frameworks that only allow single argument
    functions (e.g. lambda calculus)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Partial do?

A

With partial, we can call multi-argument functions with less arguments than they require, and obtain a specialized function with fixed arguments.

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

What does Partial return?

A

Partial may return a (possibly multi-argument) function, or value

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

What is the difference between currying and partial

A

Currying: Focus to create and return a single argument.
Partial: Can return a multi-argument function.

17
Q

Explain this code:
from functools import partial
def operation(x, y, z):
return x * (y + z)
doublesum = partial(operation, 2)
doublesum(3,1)

A

doublesum is now a more specialized function that always multiplies by 2.
Return 8

18
Q

What are Closures?

A

Closure is a function that remembers values

19
Q

What does this code show?
def outer_func():
pi = 3.14
def inner_func():
print(f”pi is : {pi}”)
return inner_func
f=outer_func()
f()

A

The outer_func function prints “pi is 3.14”

The pi variable is a nonlocal variable for the inner_func function.
This is closure!!

This code demonstrates the concept of closures in Python. A closure occurs when a nested function (here, inner_func) references a value in its enclosing scope (here, pi from outer_func), and Python allows the nested function to access that value even after the outer function has finished execution.

20
Q

Why are closures useful?

A

To avoid the use of global.

21
Q

What is Decorating?

A
  • Wrapping a function to add some “decoration” (extra functionality) to it.
  • Is is a form of composition.
22
Q

What is Currying?

A
  • Procedure to convert a multi-argument function into a chain of single-argument functions
  • The intermediate functions may be accessed for creation of specialized functions
  • It relats to lambda calculus
23
Q

What are Partials?

A
  • Runs a function with less arguments than declared in its signature
  • Main application is the creation of specialized functions (i.e. functions tied to some values)
  • Often mixed with currying
24
Q

What is a Closure?

A

A closure is a function that remembers and can access variables from the scope where it was created, even after that scope has finished executing.

25
What design patterns can be used to create specialized functions?
- Currying - Partial - Closure