for i in range(0,30,3):
print(i)
What is the functional style of the imperative loop above?
lambda x: print(x), range(0,30,3)
for i in range(0,30,3):
print(i)
What is the functional style of the imperative loop above?
lambda x: print(x), range(0,30,3)
A lambda
isn't necessary here. Just use the *
unpacking operator.
In [163]: print(*range(0, 30, 3))
0 3 6 9 12 15 18 21 24 27
If you want them printed in separate lines, that's doable too.
In [164]: print(*range(0, 30, 3), sep='\n')
0
3
6
9
12
15
18
21
24
27
if you are using python 2.x you'll need future import:
from __future__ import print_function #dont't need this for python 3.x
print(*range(0,30,3), sep='\n')
The functional way of doing things is mapping collections to functions
map(print, range(30))
However, because in python map
returns a generator, you need to somehow iterate it, which you can do by converting it to a list
list(map(print, range(30)))