Origins of Python's “Functional” Features (2009)
python-history.blogspot.com
Origins of Python's “Functional” Features (2009)
1–10 of 37 posts
Re: Origins of Python's “Functional” Features (2009)
#2Re: Origins of Python's “Functional” Features (2009)
#3I love the functional aspects of Python. It's nice to break the monotony of loops every once and while.
Re: Origins of Python's “Functional” Features (2009)
#4I love the functional aspects of Python. It's nice to break the monotony of loops every once and while.
I dunno. I love the functional parts of Groovy. But with Python I generally only break out the functional stuff when it's a choice between one unreadable line of code with a comment or else six unreadable lines of code. (E.g. when parsing an API response with weird and arbitrary nesting.)
Re: Origins of Python's “Functional” Features (2009)
#5Earlier quoted context omitted.
I dunno. I love the functional parts of Groovy. But with Python I generally only break out the functional stuff when it's a choice between one unreadable line of code with a comment or else six unreadable lines of code. (E.g. when parsing an API response with weird and arbitrary nesting.)
Which is supposed to be functional in that example?
list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets']
I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it easier to understand the program flow, and then leave a comment explaining what that line is doing.
Re: Origins of Python's “Functional” Features (2009)
#6Earlier quoted context omitted.
Which is supposed to be functional in that example?
The one line would be functional. Here is a good example: list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets'] I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it…
Re: Origins of Python's “Functional” Features (2009)
#7Earlier quoted context omitted.
Which is supposed to be functional in that example?
The one line would be functional. Here is a good example: list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets'] I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it…
[x for x in mixed_widgets if 'widgets' in x][0]['widgets']
More readable? I dunno. More "Pythonic"? Definitely.Related: I wish the list type in Python included an analogue to dict's ".get(key, default)" operation.
Re: Origins of Python's “Functional” Features (2009)
#8Earlier quoted context omitted.
The one line would be functional. Here is a good example: list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets'] I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it…
You could also use the list comprehension form of that: [x for x in mixed_widgets if 'widgets' in x][0]['widgets'] More readable? I dunno. More "Pythonic"? Definitely. Related: I wish the list type in Python included an analogue to dict's ".get(key, default)" operation.
Re: Origins of Python's “Functional” Features (2009)
#9Earlier quoted context omitted.
Which is supposed to be functional in that example?
The one line would be functional. Here is a good example: list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets'] I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it…
mixed_widgets.filter { |x| x.contains('widgets') }[0]['widgets']
Re: Origins of Python's “Functional” Features (2009)
#10Earlier quoted context omitted.
The one line would be functional. Here is a good example: list(filter(lambda x : ('widgets' in x), mixed_widgets))[0]['widgets'] I almost always go for more lines of readable code rather than less lines of unreadable code. But in cases like this, you can either write that as one line of garbage unreadable code or six lines of garbage unreadable code. So I'd rather just leave the overall codebase more dense to make it…
You could also use the list comprehension form of that: [x for x in mixed_widgets if 'widgets' in x][0]['widgets'] More readable? I dunno. More "Pythonic"? Definitely. Related: I wish the list type in Python included an analogue to dict's ".get(key, default)" operation.
(x for x in mixed_widgets if 'widgets' in x)[0]['widgets']
but this doesn't work, since you can’t do indexing [0] on a generator expression. No matter, next() returns the first value of any iterator: next(x for x in mixed_widgets if 'widgets' in x)['widgets']
Also, I think it the repetition of x in the 'x for x in' part is a bit ugly, we can fix that by moving the ['widgets'] attribute retrieval operation to inside the generator expression: next(x['widgets'] for x in mixed_widgets if 'widgets' in x)