[Tutor] Converting integers into digit sum (Python 3.3.0)

spir denis.spir at gmail.com
Tue Dec 10 19:08:47 CET 2013


On 12/10/2013 02:31 PM, Rafael Knuth wrote:
> Hej Steven,
>
> thanks for the clarification.
> I have two questions - one about map function and the other about return.
>
>> So, in mathematics we might have a mapping between (let's say) counting
>> numbers 1, 2, 3, 4, ... and the even numbers larger than fifty, 52, 54,
>> 56, ... and so on. The mapping function is 50 + 2*x:
>>
>> x = 1 --> 50 + 2*1 = 52
>> x = 2 --> 50 + 2*2 = 54
>> x = 3 --> 50 + 2*3 = 56
>> x = 4 --> 50 + 2*4 = 58
>>
>> and so on, where we might read the arrow --> as "maps to".
>>
>> So the fundamental idea is that we take a series of elements (in the
>> above case, 1, 2, 3, ...) and a function, apply the function to each
>> element in turn, and get back a series of transformed elements (52, 54,
>> 56, ...) as the result.
>>
>> So in Python, we can do this with map. First we define a function to do
>> the transformation, then pass it to map:
>>
>> def transform(n):
>>      return 50 + 2*n
>>
>> result = map(transform, [1, 2, 3, 4])
>
> #1 Question
>
> In which cases should I use a map function instead of a for loop like
> this for example:
>
> def transform(n, m):
>      for i in range (n, m):
>          print (50 + 2*i)
>
> transform(1,5)
>
>>>>
> 52
> 54
> 56
> 58

Apparently, you make a confusion (actually ) between:
* true functions, "function-functions", which produce a new piece of data (like 
math functions)
* "action-functions", which perform an effect, such as modifying the world or 
writing something to output

'map' is for true functions: it collects the sequence of products of such a 
function on a sequence of input. Therefore, map also returns a product. Right?

There is another standard tool called 'apply' in general, which sequentially 
*performms* the effect of an action on a sequence of inputs. Since it just 
applies action, 'apply' does not return any result. 'apply' is rarely used (even 
more than map; actually I think apply does not even exist in Python), because it 
is more practicle to write a simple loop. To use apply, as for map, you need to 
define a function that holds the block of code to be executed, which is often 1 
or 2 lines of code. Pretty annoying.

There is no magic in such functions as map & apply (there are also filter & 
reduce in that category), actually they are trivial. Here is an example of 
writing apply, with an example usage:

def apply (items, action):
     for item in items:
         action(item)

def show_cube (item):
     print(item, "-->", item*item*item)
items = [1,2,3,4,5]
apply(items, show_cube)

==>

1 --> 1
2 --> 8
3 --> 27
4 --> 64
5 --> 125

But as you can guess, it is far simpler to just write:

for item in items:
     print(item, "-->", item*item*item)


Now, an example of writing map, with an example usage:

def map_bis (items, func):	# different name, as 'map' exists in Python
     new_items = []
     for item in items:
         new_item = func(item)
         new_items.append(new_item)
     return new_items

def transform(n):
     return 50 + 2*n
items = [1,2,3,4,5]
new_items = map_bis(items, transform)
print(new_items)    # ==> [52, 54, 56, 58, 60]

Here, as you have seen and written yourself, map gives us a very slight 
advantage over writing the loop by hand, namely that we don't need to initialise 
the new list to []. This gives in fact another advantage, that we can chains 
maps (also with filters).
     items2 = map(f3, filter(f2, map(f1, items1)))

Apart from that, it is still *very* annoying to be forced to write 
mini-functions just for tiny blocks of codes to be used by map (or filter, 
or...). The solution in Python is comprehensions, where you write the code 
directly. Soon, you'll meet and learn about them, and probably soon later you'll 
start to love them ;-).

Denis


More information about the Tutor mailing list