-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing map(lambda()) calls with list compression #9078
Conversation
else: | ||
if MV.coords is not None: | ||
base_lst = str_combinations(base, MV.coords, rank=2, mode='__') | ||
else: | ||
base_lst = str_combinations(base, MV.subscripts, rank=2, mode='__') | ||
fct_lst = fct_sym_array(base_lst, None) | ||
self.obj = reduce(operator.add, tuple(map(lambda x: x[0] * x[1], zip(fct_lst, MV.blades[2])))) | ||
self.obj = reduce(operator.add, tuple([x[0] * x[1] for x in zip(fct_lst, MV.blades[2])])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get rid of the zip call as well?
My knowledge of list comprehensions is more theoretical than from practical use, but I dimly recall that it's possible write something like x0 * x1 for x0, x1 in fct_list, MV.blades[2]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had tried the above option along with few simple variations but kept on getting syntax errors. Hence just used zip. Does zip slower the code ? Or is to keep the code simple ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dimly recall that it's possible write something like x0 * x1 for x0, x1 in fct_list, MV.blades[2].
This is not true, that doesn't work. What you can do though is index out into x0, x1
. So the expression could read:
tuple(x0 * x1 for x0, x1 in zip(fct_lst, MV.blades[2]))
Looks good to me. However, I'm not familiar enough with list comprehensions to be 100% sure that lambda expression and list comprehension are always equivalent, so somebody else will have to verify that. I have made a few proposals for further improvement, but there's always the case of "do not fix more than one thing at a time in a PR, so it remains reviewable"; so maybe it's better to postpone these to a subsequent PR. |
@@ -1299,7 +1299,7 @@ def _eval_subs(self, old, new): | |||
if match: | |||
variables = self_vars_front + self_vars | |||
return Derivative(new, *variables) | |||
return Derivative(*map(lambda x: x._subs(old, new), self.args)) | |||
return Derivative(*[ x._subs(old, new) for x in self.args]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space between [
and x
.
Looks good, besides my one last comment. Please fix, and squash your commits into one. Will merge after that. |
A few lines were missed in boolalg:
|
Keeping PR short is a good habit. But here you are just replacing one thing with other. I think it won't be a problem to review as test suite will take care of it. |
I'm just curious to know. How did you filter? |
What should I replace the Or should I just go ahead and replace every
Ran |
Actually I think doing PRs à la "do only one thing at a time" is a good idea, even in this case. |
This would be terrible as Perhaps you are right about the intensity of the task here at play. |
Calls to
Can you give an example? It's always been my experience that I'm going to merge this now, as all |
Replacing map(lambda()) calls with list compression
Here's an example from Aaron's comment on #4940.
|
Since the "Fixes #4940" follows the auto-close syntax, the issue was closed when this was committed. If you don't want that to happen you should slip the adverb between the two as |
Sorry for that. Will be careful next time. |
I think the performance difference is negligible and a list comprehension is much more readable when the map would use a lambda. |
map(lambda(....))
is slower than list compression. Replaced about 40 such instances with list compression.Partially Fixes #4940.
@asmeurer please review.