Python higher-order function (decorator) sample

I have made various tools and web scripts with Scheme on my own machine, but there is no way that it is compatible with Scheme with cheap, high-performance, high-performance PaaS and SaaS these days, and with Python as an alternative language, Scheme We created and organized a sample for handling higher-order functions, which is one of the features.

A Higher-order Function is simply a "function that takes the function itself as an argument or a return value". For details, refer to "Structure and Interpretation of Computer Programs" "1.3 Formulating Abstractions with Higher-Order Procedures".

Definition and use of higher-order functions

Although lambda can be used in Python, it can only be handled as a single expression, so it seems that it is common to write a function that is locally defined by def.

def threetimes(f):
    def retfunc(x, y):
        print(f(f(f(x, y), y), y))
    return (retfunc)

def f(x, y):
    return (2 * x + y)

threetimes(f)(10, 5)
# => f(f(f(x, y), y), y)
# => (2 * (2 * (2 * 10 + 5) + 5) + 5) => "115"

def threetimes_message(mes = ""):
    def _threetimes(f):
        def retfunc(x, y):
            print(mes, end="")
            print(f(f(f(x, y), y), y))
        return (retfunc)
    return (_threetimes)

threetimes_message("Result = ")(f)(10, 5)
# => "Result = 115"

threetimes_message()(f)(10, 5)
# => "115"

An example of the corresponding Scheme code is as follows. Confirm the execution with Gauche.


(define threetimes
  (lambda (f)
    (lambda (x y)
      (print (f (f (f x y) y) y)))))

(define f (lambda (x y) (+ (* 2 x) y)))
((threetimes f) 10 5)
; => (f (f (f 10 5) 5) 5)
; => (+ (* 2 (+ (* 2 (+ (* 2 10) 5)) 5)) 5) => "115"

(define threetimes_message
  (lambda (f . mes)
    (lambda (x y)
      (if (not (null? mes)) (display (car mes)))
      (print (f (f (f x y) y) y)))))

((threetimes_message f "Result = ") 10 5)
; => "Result = 115"
((threetimes_message f) 10 5)
; => "115"


Syntax sugar when using higher-order functions. The etymology is a kind of design pattern. This is a convenient way to write a framework such as Flask when you want to define it as a higher-order function group and add functions to the user-defined functions that perform the original processing.

# threetimes,threetimes_message is higher order.Use py definition

def f(x, y):
    return(2 * x + y)

f(10, 5) # => "115"

@threetimes_message(mes = "Result = ")
def f(x, y):
    return(2 * x + y)

f(10, 5) # => "Result = 115"

def f(x, y):
    return (2 * x + y)

f(10, 5) # => "115"

Other samples using lambda etc.

Example in Wikipedia article (higher-order function) Some excerpts.

def args_10_5(f):
    def _args_10_5():
        f(10, 5)
    return (_args_10_5)

def f(x, y):
  print("x = ", x, ", y = ", y)

args_10_5(f)() # => "x =  10 , y =  5"

def f(x, y, z, w):
  return (4 * x + 3 * y + 2 * z + w)

f(2, 3, 4, 5) # => 30

def f(x):
    return (lambda y: lambda z: lambda w: 4 * x + 3 * y + 2 * z + w)

f(2)(3)(4)(5) # => 30

def unfold(pred, f, update, seed):
    if pred(seed):
        return ([])
        r = unfold(pred, f, update, update(seed))
        r.insert(0, f(seed))
        return (r)

unfold(lambda x: x > 10, lambda x: x * x, lambda x: x + 1, 1)
# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Recommended Posts

Python higher-order function (decorator) sample
Python function decorator
python function ①
python function ②
Note: Python Decorator
Create a Python function decorator with Class
python enumerate function
Python closure sample
Python> function> Closure
Python> function> Inner function
About function arguments (python)
python decorator to retry
Make a function decorator
Ajax + Python + PostgreSQL sample
Python function argument summary
python decorator usage notes
Python print function (sequel)
Time floor function (Python)
Python decorator operation memo
I tried Python> decorator
Python --Simple multi-thread sample
Sample data created with python
Create a function in Python
GitHub Actions Python cache sample
[python] Value of function object (?)
ntile (decile) function in python
[Python] Etymology of python function names
About the enumerate function (python)
python url setting include function
Python #function 2 for super beginners
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
Sample usage of Python pickle
[Python] Sample code for Python grammar
Python higher-order functions and comprehensions
[Python3] Define a decorator to measure the execution time of a function
[Python] What is a zip function?
Call a Python function from p5.js.
[python] Callback function (pass function as argument)
Function argument type definition in python
[Introduction to Udemy Python 3 + Application] 57. Decorator
Measure function execution time in Python
[Python] Test sample using unittest2, mock
My favorite boto3 (Python) API sample
[Python] Make the function a lambda function
Python parallel / parallel processing sample code summary
Python #len function for super beginners
Function synthesis and application in Python
Create a Python general-purpose decorator framework
Basic Python operation 2nd: Function (argument)
How to use python zip function
[Python] Difference between function and method
NAOqi Python sample (make NAO walk)
[Python] Function arguments * (star) and ** (double star)
[OpenCV; Python] Summary of findcontours function