4 ms·
You could always use tuples; they're essentially immutable lists. >>> a = tuple(3*x for x in range(5)) >>> a[2] 6 >>> map(lambda x: str(x), a) #can be
by icebraining 14y ago
You could always use tuples; they're essentially immutable lists.
>>> a = tuple(3*x for x in range(5))
>>> a[2]
6
>>> map(lambda x: str(x), a) #can be iterated
['0', '3', '6', '9', '12']
>>> a[2] = 9
TypeError: 'tuple' object does not support item assignment
>>> a.pop()
AttributeError: 'tuple' object has no attribute 'pop'
- vsbuffalo 14y agoYea, but still gross. map() isn't even an endomorphism.
- omaranto 14y agoI'm not sure what you mean. map is not supposed to be an endomorphism, rather it takes a function f and returns a list homomorphism.
- haldean 14y agoI think it'd be nice if it were an endomorphism, though; Making map(f, tuple) -> tuple, map(f, list) -> list, etc. would save users from having to write tuple(map(f, tuple)) (which is something I've done a bunch).
- omaranto 14y agoOh, you just meant you wanted lambda x: map(f,x) to be an endomorphism for any f (you said map itself should be an endomorphism which confused me). Well, it already does what you want: lambda x: map(f,x) takes an iterable and returns an iterable. :)
- haldean 14y agoFair enough (: I'm not the parent, by the way -- I was just taking a stab at what the parent might mean.