To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [expanduser(path)]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return result
See: cd
cd
, partition
partition
.
I could get rid of cd
, but this is not critical.
with cd(path):
ds, fs = partition(isdir, listdir(path))
# can be replaced with
ds, fs = partition(lambda d: isdir(join(path, d)),
listdir(path))
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [expanduser(path)]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return result
I could get rid of cd
, but this is not critical.
with cd(path):
ds, fs = partition(isdir, listdir(path))
# can be replaced with
ds, fs = partition(lambda d: isdir(join(path, d)),
listdir(path))
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [expanduser(path)]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return result
I could get rid of cd
, but this is not critical.
with cd(path):
ds, fs = partition(isdir, listdir(path))
# can be replaced with
ds, fs = partition(lambda d: isdir(join(path, d)),
listdir(path))
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [path][expanduser(path)]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(mapjoin(abspathpath, dsd) for d in ds)
result.append((path, ds, fs))
return result
I could get rid of cd
, but this is not critical.
with cd(path):
ds, fs = partition(isdir, listdir(path))
# can be replaced with
ds, fs = partition(lambda d: isdir(join(path, d)),
listdir(path))
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [path]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(map(abspath, ds))
result.append((path, ds, fs))
return result
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [expanduser(path)]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return result
I could get rid of cd
, but this is not critical.
with cd(path):
ds, fs = partition(isdir, listdir(path))
# can be replaced with
ds, fs = partition(lambda d: isdir(join(path, d)),
listdir(path))
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack[0]stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
new_stackstack.extend(join(path, =d) stack[1:]for +d map(abspath,in ds)
result.append((path, ds, fs))
return i(result, new_stackstack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [path]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(map(abspath, ds))
result.append((path, ds, fs))
return result
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack[0]
with cd(path):
ds, fs = partition(isdir, listdir(path))
new_stack = stack[1:] + map(abspath, ds)
result.append((path, ds, fs))
return i(result, new_stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [path]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(map(abspath, ds))
result.append((path, ds, fs))
return result
To learn tree traversal i implemented os.walk
, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my function so that it is maximally functional paradigm compliant? I want to keep side-effects to minimum yet not dive into deep recursions.
def walk_rec(path):
def i(result, stack):
if not stack:
return result
else:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(join(path, d) for d in ds)
result.append((path, ds, fs))
return i(result, stack)
return i([], [expanduser(path)])
def walk_iter(path):
stack = [path]
result = []
while stack:
path = stack.pop()
with cd(path):
ds, fs = partition(isdir, listdir(path))
stack.extend(map(abspath, ds))
result.append((path, ds, fs))
return result