Visualize a tree: Difference between revisions

Content added Content deleted
m (→‎Functional composition: (edited a comment line))
m (→‎Functional Python: Small edits to a couple of primitives)
Line 2,202: Line 2,202:
)
)


def lmrBuild(f, w):
def lmrBuild(w, f):
def go(wsTree):
def go(wsTree):
nChars, x = wsTree['root']
nChars, x = wsTree['root']
Line 2,382: Line 2,382:
def go(x):
def go(x):
return Node(f(x['root']))(
return Node(f(x['root']))(
list(map(go, x['nest']))
[go(v) for v in x['nest']]
)
)
return lambda tree: go(tree)
return lambda tree: go(tree)
Line 2,391: Line 2,391:
'''Right to left reduction of a list,
'''Right to left reduction of a list,
using the binary operator f, and
using the binary operator f, and
starting with an initial value a.
starting with an initial accumulator value.
'''
'''
def g(x, a):
return f(a, x)
return lambda acc: lambda xs: reduce(
return lambda acc: lambda xs: reduce(
f, xs[::-1], acc
g, xs[::-1], acc
)
)