// parallel version of the same higher-order function public void ParMapTree(TreeMapperDelegate f) { var i = 0; var tasks = new Task[3]; var t1 = Task.Factory.StartNew(() => this.Value = f(this.Value)); tasks[i++] = t1; if (Left != null) { var t2 = Task.Factory.StartNew(() => this.Left.ParMapTree(f)); tasks[i++] = t2; } if (Right != null) { var t3 = Task.Factory.StartNew(() => this.Right.ParMapTree(f)); tasks[i++] = t3; } Task.WaitAll(tasks); }
// higher-order functions over trees public void MapTree(TreeMapperDelegate f) { Value = f(Value); Left?.MapTree(f); Right?.MapTree(f); }