Notes: From Imperative to Functional, Part 1



Recently, from some functional programming exercises, I found some vague patterns from imperative to functional style. I still can't catch them definitively so decide to take some notes here first. On the other hand, while reading some MapReduce materials, I found these vague patterns are helpful for MapReduce data analyses. In short, the following is a typical example, I'll to try to collect some more examples, hopes to classify some useful patterns ... XD

To change imperative code to functional style, you need cleaner code and sensitivities of control flows. A typical example of such:

def max(visits, time):
    num = 0
    for t in range(len(visits[0])):
        if time > visits[0][t]:
            num += 1
        if time > visits[1][t]:
            num -= 1
    return num

This function can be changed to:

def max(visits, time):
    num = 0
    for t in range(len(visits[0])):
        num = num + 1 if time > visits[0][t] else num
        num = num - 1 if time > visits[1][t] else num
    return num

Although only one for loop in max, it actually deals with two problems. Basically, functional style is forcing you to decompose a problem into sub-problems. While mixed with two problems, it's not easy to write code functionally. The above function can be changed to:

def max(visits, time):
    num = 0
    for t in range(len(visits[0])):
        num = num + 1 if time > visits[0][t] else num
    for t in range(len(visits[1])):
        num = num - 1 if time > visits[1][t] else num
    return num

Then, take a look at the for loops. They're both used to calculate a result from a list. In fact, it's a typical scenario for reduce, so you can change the function to:

from functools import reduce
def max(visits, time):
    return reduce(lambda num, t: num - 1 if time > t else num, visits[1],
        reduce(lambda num, t: num + 1 if time > t else num, visits[0] , 0))