Currying
Last updated
Last updated
Function 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:
This is a "curried" series of functions that does the same thing:
Here's another example that includes the implementations:
And the same thing curried:
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, depending on the type of text file we're working with, we sometimes need to transform the font size of the text when it comes time to render it on the screen.
Fix the converted_font_size
function. We are using a 3rd party code library that expects our function to be a curried series of functions that each take a single argument.
converted_font_size
should just take a single argument, font_size
and return a function that takes a single argument, doc_type
. That function should return the font_size
multiplied by the appropriate value for the given doc_type
.
Your solution works because it implements function currying, which means it transforms a function that conceptually takes two arguments into a function that takes one argument and returns another function to accept the second argument. Hereβs a breakdown:
Curried Function Structure:
converted_font_size(font_size)
takes a single argument (font_size
) and returns a new function.
The inner function doc(doc_type)
takes a single argument (doc_type
).
Processing the Input:
When you call converted_font_size(12)
, for example, it returns the doc
function with font_size
set to 12.
Then, calling doc("md")
(i.e., converted_font_size(12)("md")
) checks the doc_type
:
If doc_type
is "txt"
, it returns font_size
(12).
If "md"
, it returns font_size * 2
(24).
If "docx"
, it returns font_size * 3
(36).
If the doc_type
is invalid, it raises a ValueError
.
Why It Meets the Assignment:
It adheres to the requirement that each function in the chain accepts a single argument.
The design lets you specify the font size first and then, depending on the document type, adjust that size with a multiplier.
This approach is useful in scenarios where you need to configure part of a process (in this case, setting a base font size) and then apply different transformations based on additional input (the document type).
It's fairly obvious that:
is simpler than:
So why would we ever want to do the more complicated thing? Well, currying is often used to change a function's signature to make it conform to a specific shape. For example:
The colorize
function accepts a function called converter
as input, and at some point during its execution, it calls converter
with a single argument. That means that it expects converter
to accept exactly one argument. So, if I have a conversion function like this:
I can't pass markdown_to_html
to colorize
because markdown_to_html
wants two arguments. To solve this problem, I can curry markdown_to_html
into a function that takes a single argument: