5 ms·
This sounds interesting. An example would really be helpful, I have a vague idea of what you're talking about but have a hard time concretizing it.
by systemvoltage 5y ago
This sounds interesting. An example would really be helpful, I have a vague idea of what you're talking about but have a hard time concretizing it.
- ajuc 5y agoYou have (psuedo)code like this: def print_every_file_in(root_path): for path in children(root_path): if is_file(path): print("file: " + path + ", size:" + get_size(path)) else: print_every_file_in(root_path) If you want to extract the printing part it's trivial in any language: def print_file(path): print("file: " + path + ", size:" + get_size(path)) def print_every_file_in(root_path): for path in children(root_path): if is_file(path): print_file(path) else: print_every_file_in(path) And you can easily parametrize what function to call on each node. But in Python it's equally trivial to extract the traversal: def every_file_in_path(root_path): for path in children(root_path): if is_file(path): yield path else: yield from every_file_in_path(path) def print_every_file_in(root_path): for file in every_file_in_path(root_path): print("file: " + path + ", size:" + get_size(path)) or even extract both: def print_every_file_in(root_path): for file in every_file_in_path(root_path): print_file(file) which for me is a very clean way to implement this and it's nontrivial to do in languages without generators.