2

Recently I am come up with google cloud function with recursive and tail recursive way of implementation. This way implement functional programming approach.

Simple Recursive Function in Python :

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

Simple Tail Recursive Function in Python :

def factorial(n, acc=1):
    if n == 0:
        return 1
    else:
        return factorial(n-1, acc*n)
Rumesh Krishnan
  • 443
  • 4
  • 16

2 Answers2

2

Cloud Functions are still just regular functions which you can recurse with. You don't need to make repeated HTTP requests, you just need to provide an argument to the function when you call it recursively that mimics the arguments that Cloud Functions will inject.

For instance, here's your first example as an HTTP trigger:

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}))

Tail recursion would be similar.

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
  • Yes. I am aware of this. Google Cloud function has 9 min max running time and if we have use case to handle the process more than that, we can use this way :) – Rumesh Krishnan Feb 08 '19 at 09:02
  • @RumeshKrishnan I have the exact use case of using recursion to work around the 9min limitation. When the condition is met - either custom condition or time taken > 8 minutes (1 min buffer from cloud function max), then send a POST request to the same cloud function. It works! But had some trouble to authenticating the call. I'm using a [solution by Shea](https://stackoverflow.com/a/63421130/9599690) to get token and adding it to the call header. – jesse May 29 '23 at 09:01
-1

Google Cloud Function - recursive function:

# 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!'

Google Cloud Function - tail recursive function:

# 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!'

There are different scenario we can use this recursive functional approach implementation in Cloud Function.!

Rumesh Krishnan
  • 443
  • 4
  • 16
  • This is quite inefficient as you'll make `n` HTTP requests each time the function is called. You'll also be billed for network egress/ingress. – Dustin Ingram Feb 07 '19 at 16:50
  • This is simple use case example, if we want to perform complex use case, we can implement retry with our error handling or reprocessing request from beginning we can do this tail recursive way.. – Rumesh Krishnan Feb 08 '19 at 09:05