

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Function Transformations
incomplete
2: Transformations Review
incomplete
3: More Transformations
incomplete
4: Why Transform?
incomplete
5: Function Transformations Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Example of a function transformation:
from collections.abc import Callable
def multiply(x: int, y: int) -> int:
return x * y
def add(x: int, y: int) -> int:
return x + y
def self_math(math_func: Callable[[int, int], int]) -> Callable[[int], int]:
# inner_func is defined inside self_math.
# It can only be referenced directly
# inside self_math's scope. However, it is then
# returned and can be captured into a new variable
# like square_func or double_func, and called that way
def inner_func(x: int) -> int:
return math_func(x, x)
return inner_func
square_func: Callable[[int], int] = self_math(multiply)
double_func: Callable[[int], int] = self_math(add)
print(square_func(5))
# 25
print(double_func(5))
# 10