Functions like sort, max, or min can take arguments like key= that are functions and can determine their behavior. This makes the built-in functions sort and max way more powerful and generic. They can be used in many more scenarios than if they would only compare the values themselves without the key= argument.
We can create our own higher-order functions as well:
Here we pass different functions as f to the higher-order function modify. The modify function creates a new list from the input one and returns the result. Note that we can even pass a function like str to the function as well. So, each element of the input list will get applied a str() function - str(2), str(0), str(1).
Challenge
Implement a generic version of the sum function called generic_sum which will take a collection as the first argument (it can be a list, a set, or a list of tuples) and a function as a second argument that obtains a number from each element of the collection.