

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: Currying
incomplete
2: Why Curry?
incomplete
3: Currying Practice
incomplete
4: Currying Practice
incomplete
5: Currying Practice
incomplete
This lesson's interactive features are locked, please to keep using them
Function currying is a specific kind of function transformation, where we translate a single function that accepts multiple arguments into multiple functions that each accept a single argument.
This is a "normal" 3-argument function:
box_volume(3, 4, 5)
This is a "curried" series of functions that does the same thing:
box_volume(3)(4)(5)
Here's another example that includes the implementation:
def sum(a: int, b: int) -> int:
return a + b
print(sum(1, 2))
# prints 3
And the same thing curried:
from collections.abc import Callable
def sum(a: int) -> Callable[[int], int]:
def inner_sum(b: int) -> int:
return a + b
return inner_sum
print(sum(1)(2))
# prints 3
The sum function only takes a single input, a. It returns a new function that takes a single input, b. This new function, when called with a value for b, will return the sum of a and b. We'll talk later about why this is useful.
In Doc2Doc, for some types of text files, we need to transform the font size of the text when rendering it onscreen.
Fix the converted_font_size function. We're using a third-party code library that expects our function to be a curried series of functions that each take a single argument.
You can always click the "Reset lesson" button to restore the correct font_size multipliers, if you accidentally change them.