85. var foldMap = function(f,xs) {
return compose(fold, map(f))(xs);
}
var sum = function(xs){ return foldMap(Sum, xs).val }
var max = function(xs){ return foldMap(Max, xs).val }
var any = function(xs){ return foldMap(Any, xs).val }
86. var tree = Node(Node(Leaf(2), 1, Leaf(3)), 2, Leaf(4))
sum(tree)
//=> 12
product(tree)
//=> 38
max(tree)
//=> 4
124. var interpret = function(t) {
switch (t.constructor) {
case _Concat: return t.next.concat(t.val);
case _Replace: return t.next.replace(t.val, t.x);
case _Input: return t.val;
}
}
var prog = Concat("world", Replace("h", "m", Input(“hello")))
cata(interpret, prog)
//=> melloworld
125. var interpret1 = function(t) {
switch (t.constructor) {
case _Concat:
return "concatting “+t.val+" after "+t.next;
case _Replace:
return "replacing "+t.val+" with "+t.x+" on "+t.next;
case _Input:
return t.val;
}
}
var prog = Concat("world", Replace("h", "m", Input(“hello")))
cata(interpret1, prog)
//=> concatting world after replacing h with m on hello