Project

General

Profile

« Previous | Next » 

Revision 1882

collection.py: rmap(): Fixed bugs: Made it recursive. Use iters.is_iterable() instead of isinstance(value, list) to work on all iterables. Use value and not nonexistent var list_.

View differences:

collection.py
1 1
# Data structures
2 2

  
3
import iters
4

  
3 5
def rmap(func, value):
4 6
    '''Recursively applies func to all members of value'''
5
    if isinstance(value, list): return map(func, list_)
6
    elif isinstance(value, dict):
7
        return dict(((k, func(v)) for k, v in value.iteritems()))
7
    rmap_ = lambda v: rmap(func, v)
8
    if isinstance(value, dict):
9
        return dict(((k, rmap_(v)) for k, v in value.iteritems()))
10
    elif iters.is_iterable(value): return map(rmap_, value)
8 11
    else: return func(value)

Also available in: Unified diff