grains.utils.map_inplace

grains.utils.map_inplace(function, __iterable)[source]

Apply a function to each member of an iterable in-place.

Parameters
  • function (function object) – Function to be applied to the entries of the iterable.

  • __iterable (iterable) – Iterable.

Notes

Comprehensions or functional tools work on iterators, thereby not modifying the original container (https://stackoverflow.com/a/4148523/4892892). For in-place modification, the conventional for loop approach is used (https://stackoverflow.com/a/4148525/4892892).

Examples

>>> lst = ['2', 2]; func = lambda x: x*2
>>> map_inplace(func, lst); lst
['22', 4]
>>> lifespan = {'cat': 15, 'dog': 12}; die_early = lambda x: x/2
>>> map_inplace(die_early, lifespan); lifespan
{'cat': 7.5, 'dog': 6.0}