Functional programming

Functional Programming (FP) is a development approach centered around the concept of passing control through a series of pure functions, and avoiding shared state and mutable data structures. That is, the majority of any real functionality in FP is wrapped in functions that will always return the same output for any given input, and don't modify any external variables. Technically, a pure function should not write data to anywhere—neither logging to a console or file, nor writing to a file—and how the need for that sort of output is accommodated is a discussion well outside the scope of this book.

The following is the same functionality that was in the previous two examples, but written using a Functional Programming approach (if only barely, since the task it's performing isn't all that complex):

#!/usr/bin/env python
"""
An example of a simple FP-based program. Asks the user for a URL,
retrieves the content of that URL, writes it to a temp-file, and
repeats until the user tells it to stop.
"""

# Importing stuff we'll use
import os

import urllib.request

if os.name == 'posix':
tmp_dir = '/tmp/'
else:
tmp_dir = 'C:\Temp'
if not os.path.exists(tmp_dir):
os.mkdirs(tmp_dir)

# Defining our functions

def get_page_data(url):
page = urllib.request.urlopen(url)
page_data = page.read()
page.close()
return page_data

def save_page_data(local_file, page_data):
with open(local_file, 'w') as out_file:
out_file.write(str(page_data))
return('Page-data written to %s' % (local_file))

def get_local_file(url):
return ('%s%s.data' % (tmp_dir, ''.join(
[c for c in the_url if c not in ':/']
)
)).replace('https', '').replace('http', '')

def process_page(url):
return save_page_data(
get_local_file(url), get_page_data(url)
)

def get_page_to_process():
the_url = input(
'Please enter a URL to read, or "X" to cancel: '
)
if the_url:
return the_url.lower()
return None

if __name__ == '__main__':
# Again, almost the same loop...
the_url = get_page_to_process()
while the_url not in ('x', None):
print(process_page(the_url))
the_url = get_page_to_process()
print('Exiting. Thanks!')

Again, this code performs the exact same function, and it does so with the same discrete steps/processes as the previous two examples. It does so, however, without having to actually store any of the various data it's using—there are no mutable data elements in the process itself, only in the initial input to the process_page function, and even then, it's not usefully mutable for very long. The main function, process_page, also doesn't use any mutable values, just the results of other function calls. All of the component functions return something, even if it's only a None value.

Functional Programming is not a new paradigm, but it hasn't become widely accepted until relatively recently. It has the potential to be as fundamentally disruptive as Object-Oriented Programming was. It's also different, in many respects, so that making a transition to it might well be difficult—it relies, after all, on substantially different approaches, and on a stateless basis that is very atypical in or of other modern development paradigms. That stateless nature, though, and the fact that it enforces a rigid sequence of events during execution, have the potential to make FP-based code and processes much more stable than their OO or procedural counterparts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.186.92