- Hands-On Software Engineering with Python
- Brian Allbee
- 510字
- 2021-06-24 18:20:38
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.
- Spark編程基礎(chǔ)(Scala版)
- IoT Penetration Testing Cookbook
- 群體智能與數(shù)據(jù)挖掘
- 傳感器技術(shù)應(yīng)用
- Data Wrangling with Python
- 數(shù)據(jù)挖掘方法及天體光譜挖掘技術(shù)
- 21天學(xué)通Visual Basic
- AI 3.0
- 信息物理系統(tǒng)(CPS)測(cè)試與評(píng)價(jià)技術(shù)
- 變頻器、軟啟動(dòng)器及PLC實(shí)用技術(shù)260問(wèn)
- 內(nèi)模控制及其應(yīng)用
- Azure PowerShell Quick Start Guide
- The DevOps 2.1 Toolkit:Docker Swarm
- 一步步寫嵌入式操作系統(tǒng)
- Python語(yǔ)言從入門到精通