3.6. FuncProg Higher-Order

3.6.1. Rationale

  • Function can take other function as arguments

  • Function can return function

3.6.2. Example

def lower():
    ...


def higher():
    return lower

3.6.3. Use Case

def http_request(url, on_success, on_error):
    try:
        result = ...
    except Exception as error:
        return on_error(error)
    else:
        return on_success(result)


http_request(
    url = 'https://python.astrotech.io',
    on_success = lambda result: print(result),
    on_error = lambda error: print(error))