microsoft / knossos-ksc

Compiler with automatic differentiation
Other
45 stars 10 forks source link

Use iterator to directly find element, rather than loop #982

Closed toelli-msft closed 2 years ago

toelli-msft commented 2 years ago

Would it be OK to switch to this more direct style of finding the matching element? In the existing the logic is mixed up with the control flow which made it harder for me to understand.

Added: yield elements rather than manually collecting in a list. Unfortunately the only two options for turning the generator function into a reversed list that I know are list(ksc_defs())[::-1] and list(reversed(list(ksc_defs()))), both of which seem less clear than we might hope.

dcrc2 commented 2 years ago

Can we move the call to inspect.getmembers out of the loop? I'd suggest creating module_fns = dict(inspect.getmembers(module)) which simplifies the lookup later.

toelli-msft commented 2 years ago

Oh, using a dict is a much better idea anyway! I'll do that instead.