Python - Functional Programming

> Procedural Languages > Python > Python - Functional Programming

1 - About

2 - Example

2.1 - Class

# Same syntax as PySpark
class FunctionalWrapper(object):
    def __init__(self, data):
        self.data = data
    def map(self, function):
        """Call `map` on the items in `data` using the provided `function`"""
        return FunctionalWrapper(map(function, self.data))
    def reduce(self, function):
        """Call `reduce` on the items in `data` using the provided `function`"""
        return reduce(function, self.data)
    def filter(self, function):
        """Call `filter` on the items in `data` using the provided `function`"""
        return FunctionalWrapper(filter(function, self.data))
    def __eq__(self, other):
        return (isinstance(other, self.__class__)
            and self.__dict__ == other.__dict__)
    def __getattr__(self, name):  return getattr(self.data, name)
    def __getitem__(self, k):  return self.data.__getitem__(k)
    def __repr__(self):  return 'FunctionalWrapper({0})'.format(repr(self.data))
    def __str__(self):  return 'FunctionalWrapper({0})'.format(str(self.data))

2.2 - Without chaining

Implementation of map, filter and sum with lambda expression

dataset = FunctionalWrapper(range(10))
 
# Multiply each element by 5
mapResult = dataset.map(lambda x: x*5)
# Keep the even elements
filterResult = dataset.filter(lambda x: x % 2 == 0)
# Sum the elements
reduceResult = dataset.reduce(lambda a,b:a+b)
Advertising

2.3 - Function Chaining

Since the methods for map and filter in the FunctionalWrapper class return FunctionalWrapper objects, we can compose (or chain) together the function calls.

# Example of a mult-line expression statement
# Note that placing parentheses around the expression allow it to exist on multiple lines without
# causing a syntax error.
(dataset
 .map(lambda x: x + 2)
 .reduce(lambda x, y: x * y))