Functional coverage tree: Difference between revisions
m
→Python: Composition of pure functions: Tidied, updated primitives.
(Added Wren) |
m (→Python: Composition of pure functions: Tidied, updated primitives.) |
||
Line 2,392:
delimiter = '|'
reportLines =
columnTitles = init(columnNames(delimiter)(reportLines[0]))
# ------ SERIALISATION OF DECORATED PARSE TREE -------
print(titleLine(delimiter)(columnWidths)(
columnTitles + ['share of residue']
Line 2,401:
print(indentedLinesFromTree(' ', tabulation(columnWidths))(
# -------- TWO COMPUTATIONS BY TRAVERSAL ---------
withResidueShares(1.0)(
foldTree(weightedCoverage)(
# --- TREE FROM PARSE OF OUTLINE TEXT ----
fmapTree(
recordFromKeysDefaultsDelimiterAndLine
)(
[str, float, float])([
'?', 1.0, 0.0
])(delimiter)
)(
)
)[0]
)
)
Line 2,419 ⟶ 2,425:
# ---- WEIGHTED COVERAGE, AND SHARE OF TOTAL RESIDUE
# weightedCoverage :: Tree Dict ->
Line 2,429 ⟶ 2,435:
'''
def go(xs):
cws = [
(r['coverage'], r['weight']) for r
in [root(x) for x in xs]
]
totalWeight = reduce(lambda a, x: a + x[1], cws, 0)
return Node(dict(
Line 2,439 ⟶ 2,448:
}
))(xs)
return
Line 2,448 ⟶ 2,457:
'''
def go(fraction, node):
[nodeRoot, nodeNest] =
weights = [root(x)['weight'] for x in nodeNest]
siblingsTotal = sum(weights)
Line 2,465 ⟶ 2,474:
#
# tabulation :: [Int] -> String -> Dict -> String
Line 2,495 ⟶ 2,504:
#
# Node :: a -> [Tree a] -> Tree a
Line 2,506 ⟶ 2,515:
#
def
'''The application of each of a list of functions,
to each of a list of values.
'''
def go(xs):
return [
f(x) for (f, x)
in product(fs, xs)
]
return go
Line 2,548 ⟶ 2,552:
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated list
The list monad can be derived by using a function f which
(using an empty list to represent computational failure).
'''
def go(xs):
return chain.from_iterable(map(f, xs))
Line 2,565 ⟶ 2,568:
# first :: (a -> b) -> ((a, c) -> (b, c))
def first(f):
'''A simple function lifted to a function over a tuple,
with f applied only the first of two values.
'''
return lambda xy:
Line 2,593 ⟶ 2,595:
def fmapTree(f):
'''A new tree holding the results of
the existing tree.
'''
def go(x):
return Node
)([go(v) for v in x['nest']])
return
Line 2,606 ⟶ 2,608:
def foldTree(f):
'''The catamorphism on trees. A summary
value
'''
def go(node):
return f(
go(x) for x in
])
return
#
def
'''A list of trees derived from a list of
with integers giving their levels of indentation.
'''
def go(xs):
if xs:
)(xs[1:])
return [Node(
else:
return []
Line 2,636 ⟶ 2,638:
'''First member of a pair.'''
return tpl[0]
Line 2,651 ⟶ 2,647:
'''
indentTextPairs = list(map(
compose(
xs
))
Line 2,658 ⟶ 2,654:
)(indentTextPairs))
return list(map(
indentTextPairs
))
Line 2,670 ⟶ 2,666:
'''
def go(indent):
return lambda node: [f(indent, node['root'])] +
)
return lambda tree: '\n'.join(go('')(tree))
Line 2,699 ⟶ 2,697:
'''
return s.isspace()
Line 2,770 ⟶ 2,748:
span p xs is equivalent to (takeWhile p xs, dropWhile p xs).
'''
def match(ab):
b = ab[1]
return not b or not p(b[0])
def f(ab):
a, b = ab
return a + [b[0]], b[1:]
def go(xs):
# splitOn :: String -> String -> [String]
def splitOn(pat):
Line 2,786 ⟶ 2,768:
return lambda xs: (
xs.split(pat) if isinstance(xs, str) else None
)
Line 2,811 ⟶ 2,775:
'''String in lower case.'''
return s.lower()
Line 2,826 ⟶ 2,782:
The initial seed value is x.
'''
def go(f
return g
return go
|