Skip to content

Instantly share code, notes, and snippets.

@acdimalev
Created July 6, 2019 08:50
Show Gist options
  • Save acdimalev/667319d83ce10adf6e0d1724871dbc86 to your computer and use it in GitHub Desktop.
Save acdimalev/667319d83ce10adf6e0d1724871dbc86 to your computer and use it in GitHub Desktop.
Fun with directory recursion and Python
fix = lambda f: lambda x: f(fix(f), x)
# presume Python's obsession with iterators...
join = __import__('itertools').chain.from_iterable
bind = lambda f, xs: join(map(f, xs))
# I/O primitives...
(subpaths, subdirs) = (lambda os: (
lambda path: [os.path.join(path, x) for x in os.listdir(path)],
lambda path: [x for x in subpaths(path) if os.path.isdir(x)],
))(__import__('os'))
# subdirs of subdirs...
step_subdirs = lambda f, dirs: dirs and dirs + f(list(bind(subdirs, dirs)))
recurse_subdirs = fix(step_subdirs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment