Higher-order Functions
Flattening data while mapping
In Chapter 4, Working with Collections, we looked at algorithms that flattened a nested
tuple-of-tuples structure into single iterable. Our goal at the time was simply to
restructure some data, without doing any real processing. We can create hybrid
solutions that combine a function with a flattening operation.
Let's assume that we have a block of text that we want to convert to a flat sequence
of numbers. The text looks as follows:
text= """\
2 3 5 7 11 13 17 19 23
29
31 37 41 43 47 53 59 61 67
71
73 79 83 89 97 101 103 107 109
113
127 131 137 139 149 151 157 163 167
173
179 181 191 193 197 199 211 223 227
229
"""
Each line is a block of 10 numbers. We need to unblock the rows to create a flat
sequence of numbers.
This is done with a two part generator function as follows:
data= list(v for line in text.splitlines() for v in line.split())
This will split the text into lines and iterate through each line. It will split each line
into words and iterate through each word. The output from this is a list of strings,
as follows:
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37',
'41', '43', '47', '53', '59', '61', '67', '71', '73', '79', '83',
'89', '97', '101', '103', '107', '109', '113', '127', '131', '137',
'139', '149', '151', '157', '163', '167', '173', '179', '181', '191',
'193', '197', '199', '211', '223', '227', '229']
To convert the strings to numbers, we must apply a conversion function as well
as unwind the blocked structure from its original format, using the following
code snippet: