【问题标题】:How to implement Google Cloud Function using Functional Programming approach?如何使用函数式编程方法实现 Google Cloud Function?
【发布时间】:2019-02-07 16:12:39
【问题描述】:

最近我想出了具有递归和尾递归实现方式的谷歌云功能。这种方式实现了函数式编程方法。

Python 中的简单递归函数:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

Python 中的简单尾递归函数:

def factorial(n, acc=1):
    if n == 0:
        return 1
    else:
        return factorial(n-1, acc*n)

【问题讨论】:

    标签: python-3.x google-cloud-platform google-cloud-functions tail-recursion


    【解决方案1】:

    云函数仍然只是可以递归的常规函数​​。您无需发出重复的 HTTP 请求,只需在递归调用该函数时提供一个参数来模拟 Cloud Functions 将注入的参数。

    例如,这是您作为 HTTP 触发器的第一个示例:

    class MockRequest:
        def __init__(self, args):
            self.args = args
    
    def factorial(request):
        n = request.args.get('n')
        if n == 0:
            return 1
        else:
            return n * factorial(MockRequest({'n': n-1}))
    

    尾递归也是类似的。

    【讨论】:

    • 是的。我知道这一点。谷歌云函数的最大运行时间为 9 分钟,如果我们有用例来处理更多的过程,我们可以使用这种方式:)
    【解决方案2】:

    谷歌云函数 - 递归函数:

    # import request
    import urllib.request as req
    
    def recursive(request):
        url = "https://<google-cloud-function-domain-url>/function-rec"
        # check the url arg `n` 
        if request.args and 'n' in request.args:
            n = int(request.args.get('n'))
            if n <= 0:
                return str(0)
            else:
                complete_url = url + "?n=" + str(n-1)
                n_1_result = req.urlopen(complete_url).read()
                return str(n + int(n_1_result))
        else:
            return f'Please send the `n` value in the argument!'
    

    谷歌云函数 - 尾递归函数:

    # import redirect
    from flask import redirect
    
    def recursive(request):
        url = "https://<google-cloud-function-domain-url>/function-rec-redirect"
        # check the url arg `n` and `acc` else if with `n` arg
        if request.args and 'n' in request.args and 'acc' in request.args:
            n = int(request.args.get('n'))
            acc = int(request.args.get('acc'))
            if n <= 0:
                return str(acc)
            else:
                complete_url = url + "?n=" + str(n-1) + "&acc=" + str(n + acc)
                return redirect(complete_url, code=307)
        elif request.args and 'n' in request.args:
            n = int(request.args.get('n'))
            if n <= 0:
                return str(0)
            else:
                complete_url = url + "?n=" + str(n-1) + "&acc=" + str(n)
                return redirect(complete_url, code=307)
        else:
            return f'Please send the `n` value in the argument!'
    

    我们可以在 Cloud Function 中使用这种递归函数方法实现的不同场景。!

    【讨论】:

    • 这非常低效,因为每次调用该函数时都会发出n HTTP 请求。您还将为网络出口/入口付费。
    • 这是一个简单的用例示例,如果我们要执行复杂的用例,我们可以通过我们的错误处理或重新处理请求来实现重试,我们可以使用这种尾递归方式..
    猜你喜欢
    • 2019-06-24
    • 2020-10-25
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2019-05-14
    • 2018-04-23
    • 2019-10-24
    • 2020-09-25
    相关资源
    最近更新 更多