Function Transformations
Last updated
Last updated
"Function transformation" is just a more concise way to describe a specific type of . It's when a function takes a function (or functions) as input and returns a new function. Let's look at an example:
The self_math
function takes a function that operates on two different parameters (e.g. multiply
or add
) and returns a new function that operates on one parameter twice (e.g. square
or double
).
Doc2Doc needs a good logging system so that users and developers alike can see what's going on under the hood. Complete the get_logger
function.
It takes a formatter
function as a parameter and returns a new function. Steps:
Define a new function, logger
, inside get_logger
(see self_math
above as an example). It accepts two strings. You can just name them first
and second
if you like.
The logger
function should not return anything. It should simply print
the result of calling the given formatter
function with the first
and second
strings as arguments.
Return the new logger
function for the test suite to use.
The colon_delimit
and dash_delimit
functions are "formatters" that will be passed into our get_logger
function by the tests. You don't need to touch them, but it's important to understand that when you call formatter()
in the get_logger
function, you're calling one of these functions.
Inside get_logger
, you define another function logger
that accepts two arguments (first
and second
).
The logger
function simply calls the formatter
function with the two arguments (first
and second
) and prints the result. After defining logger
, get_logger
returns this function so that it can be used elsewhere (such as in the test suite).
In essence:
get_logger
returns a function (logger
) that uses the provided formatter
.
When the returned logger
function is called, it prints the result of calling formatter
with the provided arguments.
This structure allows flexibility, where different formatter
functions can be passed to get_logger
to customize the output formatting.