From 5ff17c8fc0fb09b7ff58e8e45113738547b2b9b1 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 2 Oct 2015 17:11:02 -0400 Subject: [PATCH] Syntax.Branch holds an array again. --- prototype/Doubt/Diff.swift | 2 +- prototype/Doubt/Syntax.swift | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/prototype/Doubt/Diff.swift b/prototype/Doubt/Diff.swift index a45948f9e..c2e531cb7 100644 --- a/prototype/Doubt/Diff.swift +++ b/prototype/Doubt/Diff.swift @@ -106,7 +106,7 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible self = .Copy(.Leaf(v2)) case let (.Branch(v1), .Branch(v2)): - self = .Copy(.Branch(Diff(v1, v2))) + self = .Copy(.Branch(Diff.diff(v1, v2))) default: self = .Patch(a, b) diff --git a/prototype/Doubt/Syntax.swift b/prototype/Doubt/Syntax.swift index a97ac1e7b..cf94f09c8 100644 --- a/prototype/Doubt/Syntax.swift +++ b/prototype/Doubt/Syntax.swift @@ -32,21 +32,21 @@ public func == (left: Term, right: Term) -> Bool { /// A node in a syntax tree. Expressed algebraically to enable representation of both normal syntax trees and their diffs. public enum Syntax: CustomDebugStringConvertible, CustomDocConvertible { case Leaf(A) - case Branch(Recur) + case Branch([Recur]) public func map(@noescape transform: Recur -> T) -> Syntax { switch self { case let .Leaf(n): return .Leaf(n) case let .Branch(x): - return .Branch(transform(x)) + return .Branch(x.map(transform)) } } public func reduce(initial: T, @noescape combine: (T, Recur) throws -> T) rethrows -> T { switch self { case let .Branch(x): - return try combine(initial, x) + return try x.reduce(initial, combine: combine) default: return initial @@ -67,7 +67,7 @@ public enum Syntax: CustomDebugStringConvertible, CustomDocConvertible case let .Leaf(n): return Doc(n) case let .Branch(x): - return Doc(x) + return x.map(Doc.init).joinWithSeparator(", ").bracket("[", "]") } } } @@ -81,7 +81,7 @@ extension Syntax { case let (.Leaf(l1), .Leaf(l2)): return ifLeaf(l1, l2) case let (.Branch(v1), .Branch(v2)): - return ifRecur(v1, v2) + return v1.count == v2.count && zip(v1, v2).lazy.map(ifRecur).reduce(true) { $0 && $1 } default: return false } @@ -105,7 +105,7 @@ extension Syntax { case let .Leaf(n): return Hash("Leaf", ifLeaf(n)) case let .Branch(x): - return Hash("Branch", ifRecur(x)) + return Hash("Branch", .Ordered(x.map(ifRecur))) } } }