4.2.1 Map
my_map
takes in a one argument function fn
and a sequence seq
and returns a list containing fn
applied to each element in seq
.
Use only a single line for the body of the function. (Hint: use a list comprehension.)
def my_map(fn, seq):
"""Applies fn onto each element in seq and returns a list.
>>> my_map(lambda x: x*x, [1, 2, 3])
[1, 4, 9]
>>> my_map(lambda x: abs(x), [1, -1, 5, 3, 0])
[1, 1, 5, 3, 0]
>>> my_map(lambda x: print(x), ['sicp', 'autumn', '2023'])
sicp
autumn
2023
[None, None, None]
"""
return ______