Welcome, guest | Sign In | My Account | Store | Cart

The Python map() function returns a list of the results of applying the function to the items of the argument sequence(s).

The fmap() function does the inverse, in a sense. It returns the result of applying a list of functions to a given argument.

Python, 82 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# fmap.py

# Author: Vasudev Ram - http://www.dancingbison.com

# fmap() is a Python function which is a kind of inverse of the 
# built-in Python map() function.
# The map() function is documented in the Python interpreter as
# follows:

"""
>>> print map.__doc__
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s).  If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length.  If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
"""

# The fmap() function does the inverse, in a sense.
# It returns the result of applying a list of functions to a 
# given argument.
# TODO: Later extend the function to also work on a sequence of 
# arguments like map() does.

import string

def fmap(function_list, argument):
 result = argument
 for function in function_list:
  #print "calling " + function.__name__ + "(" + repr(result) + ")"
  result = function(result)
 return result

def times_two(arg):
 return arg * 2

def square(arg):
 return arg * arg

def upcase(s):
 return string.upper(s)

def delspace(s):
 return string.replace(s, ' ', '')

def main():

 print

 function_list = [ times_two, square ]
 for argument in range(5):
  fmap_result = fmap(function_list, argument)
  print "argument:", argument, ": fmap result:", fmap_result

 print

 function_list = [ upcase, delspace ]
 for argument in [ "the quick brown fox", "the lazy dog" ]:
  fmap_result = fmap(function_list, argument)
  print "argument:", argument, ": fmap result:", fmap_result

if __name__ == "__main__":
 main()

# EOF: fmap.py

"""
Output of running a test program for fmap():
$> python fmap.py

argument: 0 : fmap result: 0
argument: 1 : fmap result: 4
argument: 2 : fmap result: 16
argument: 3 : fmap result: 36
argument: 4 : fmap result: 64

argument: the quick brown fox : fmap result: THEQUICKBROWNFOX
argument: the lazy dog : fmap result: THELAZYDOG
"""

Blog post giving more details and example output:

http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html

3 comments

pavel 11 years, 6 months ago  # | flag

Hi Vasudev,

do you like functional programming? See my implementation:

fmap = lambda fnlist, argument: reduce( lambda x, fn: fn(x), [ argument ] + fnlist )
Vasudev Ram (author) 11 years, 6 months ago  # | flag

That looks interesting, Pavel.

Thanks for commenting.

Vasudev Ram (author) 11 years, 6 months ago  # | flag

Also check out my blog post linked in the article above, if you haven't already. There is another implementation of fmap in the comments, by a reader.

Created by Vasudev Ram on Sat, 6 Oct 2012 (PSF)
Python recipes (4591)
Vasudev Ram's recipes (93)

Required Modules

  • (none specified)

Other Information and Tasks