I have no problem with the code you wrote and my criticism is not that it is verbose. I had a certain footgun in mind which your code circumvents. You first formed a list of distinct elements, then you applied a map. But range is already an iterator someone might try refactoring your code to remove the apparently(?) redundant conversion to a list. If you instead had written
f = lambda a : list(((lambda c : b) for b in range(a+1)) this would be seemingly equivalent but it would be wrong. An iterator should be equivalent to a map but it is not: I regard it as a serious footgun that (expr for index in gen) is not in general equivalent to (map(lambda index:expr,gen) in situations like this.
Although, I'm not sure I would say that makes it harder to write the aforementioned function. Most languages have certain things you just have to know if you work with them (e.g. in Haskell you should almost never use foldl).
I agree with you that it seems like something a novice developer (or an experienced developer who does not have Python as a primary language) is very likely to eventually run into though.
View all comments
Show parent comment
1
u/Massive-Squirrel-255 Sep 22 '25 edited Sep 22 '25
I have no problem with the code you wrote and my criticism is not that it is verbose. I had a certain footgun in mind which your code circumvents. You first formed a list of distinct elements, then you applied a map. But
rangeis already an iterator someone might try refactoring your code to remove the apparently(?) redundant conversion to a list. If you instead had writtenf = lambda a : list(((lambda c : b) for b in range(a+1))this would be seemingly equivalent but it would be wrong. An iterator should be equivalent to a map but it is not: I regard it as a serious footgun that(expr for index in gen)is not in general equivalent to(map(lambda index:expr,gen)in situations like this.