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( |
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']))( |
||
[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 |
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( |
||
g, xs[::-1], acc |
|||
) |
) |
||