Skip to content Skip to sidebar Skip to footer

Using Map With None Type Functions

exists in python an equivalent to Haskell's mapM_ function? example ('print()' is here only a placeholder for every function with Signature: a -> None): #pseudo code!!! map (lam

Solution 1:

map just creates a lazy iterator. You would have to consume such an iterator (e.g. by calling list on it) in order to harvest the side-effects. Since that wastes memory (as do other collection constructors), the recommended consume recipe uses a zero size collections.deque:

from collections import deque

def consume(iterator):
    deque(iterator, maxlen=0)

Now, you can consume an iterator with a small memory footprint:

lazy = map(print, [1,2,3])  # using lambda here defeats the purpose of functions
consume(lazy)

But TMK this provides little advantage over a simple loop:

for x in [1, 2, 3]:
    print(x)

Post a Comment for "Using Map With None Type Functions"