ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] Higher-Order Functions
    TIL 2022. 6. 19. 22:02
    • In Python, all functions are classified as first-class objects.
      • This means they have four important characteristics:
    1. First-class objects can be stored as variables.
    2. First-class objects can be passed as arguments to a function.
    3. First-class objects can be returned by a function.
    4. First-class objects can be stored in data structures (e.g., lists, dictionaries, etc.).
    def make_box_volume(height):
        # defines and returns a function that takes two numeric arguments,        
        # length &  width, and returns the volume given the input height
        def volume(length, width):
            return length*width*height
     	
        # volume() is declared in the scope of make_box_volume and cannot be called outside parent function
        
        return volume
     
    box_volume_height15 = make_box_volume(15)
    print(box_volume_height15(3,2))
    # output is 90
    
    box_volume_height10 = make_box_volume(10)
    print(box_volume_height10(3,2))
    # output is 60

    • As mentioned above, functions can be passed as arguments to other functions.
    • We can therefore create decorator functions to tweak the functionality of our original function.
    def smart_divide(func):
        def inner(a, b):
            if b == 0:
                print("Cannot devide by zero!")
                return
            return func(a, b)
        return inner
        
    # this is equivalent to smart_devide(divide(a, b))
    @smart_divide
    def divide(a, b):
        print(a/b)

    • Lambda functions are the preferred way of creating one-line functions.
      • The reduced syntax assists code readability and the functions can be implemented where code reuse is not the primary objective.
      • If we wanted our function complexity to extend beyond one line, we would opt for a regular function since making our function longer would impair readability.
    check_if_A_grade = lambda grade: 'Got an A!' if grade >= 90 else 'Did not get an A.'

    This is what the above line of code does:

    1. lambda grade: declares a lambda function with the parameter grade
    2. Return 'Got an A!' if the statement: grade >=90 is true:
    3. Otherwise, return 'Did not get an A.'

    • Higher-order functions map() and filter() have the following base structure:
    returned_map_object = map(function, iterable)
    returned_filter_object = filter(function, iterable)

    • We can use lambda functions in conjunction with these higher-order functions
    int_list = [3, 6, 9]
    
    doubled = map(lambda x: x*2, int_list)
    print(doubled)
    # output = <map at 0x7f1ca0f58090>
    print(list(doubled))
    # output = [6, 12, 18]
    
    ------------------------------------------------------------------------
    
    names = ["margarita", "Linda", "Masako", "Maki", "Angela"]
     
    M_names = filter(lambda name: name[0] == "M" or name[0] == "m", names)
    print(list(M_names))
    # output = ['margarita', 'Masako', 'Maki']
Designed by Tistory.