1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 04:10:29 +03:00

🔥 Vertex & VertexTests.

This commit is contained in:
Rob Rix 2015-10-06 14:24:56 -04:00
parent 9b6a3f6e36
commit 0a6a136bff
3 changed files with 0 additions and 182 deletions

View File

@ -28,8 +28,6 @@
D49FCBC41BBEF98E00C5E9C3 /* Free.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49FCBC31BBEF98E00C5E9C3 /* Free.swift */; settings = {ASSET_TAGS = (); }; };
D49FCBC61BBF214300C5E9C3 /* Patch.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49FCBC51BBF214300C5E9C3 /* Patch.swift */; settings = {ASSET_TAGS = (); }; };
D49FCBC81BBF2C4300C5E9C3 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49FCBC71BBF2C4300C5E9C3 /* Algorithm.swift */; settings = {ASSET_TAGS = (); }; };
D4A71DC51BB45B850051416D /* Vertex.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4A71DC41BB45B850051416D /* Vertex.swift */; settings = {ASSET_TAGS = (); }; };
D4A71DC71BB4AC9E0051416D /* VertexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4A71DC61BB4AC9E0051416D /* VertexTests.swift */; settings = {ASSET_TAGS = (); }; };
D4AAE50E1B5AE22E004E581F /* Doubt.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4AAE4FD1B5AE22E004E581F /* Doubt.framework */; };
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5351B5AE2D0004E581F /* Array.swift */; };
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5371B5AE2D0004E581F /* Doc.swift */; };
@ -110,8 +108,6 @@
D49FCBC31BBEF98E00C5E9C3 /* Free.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Free.swift; sourceTree = "<group>"; };
D49FCBC51BBF214300C5E9C3 /* Patch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Patch.swift; sourceTree = "<group>"; };
D49FCBC71BBF2C4300C5E9C3 /* Algorithm.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = "<group>"; };
D4A71DC41BB45B850051416D /* Vertex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vertex.swift; sourceTree = "<group>"; };
D4A71DC61BB4AC9E0051416D /* VertexTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VertexTests.swift; sourceTree = "<group>"; };
D4AAE4FD1B5AE22E004E581F /* Doubt.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Doubt.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4AAE5011B5AE22E004E581F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D4AAE50D1B5AE22E004E581F /* DoubtTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DoubtTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@ -209,7 +205,6 @@
D432D4741BA9D6A400F3FABC /* Memo.swift */,
D4413FEE1BB06D4C00E3C3C1 /* Dictionary.swift */,
D4413FF01BB08FDC00E3C3C1 /* JSON.swift */,
D4A71DC41BB45B850051416D /* Vertex.swift */,
D40B89C31BC319070078E098 /* Matrix.swift */,
D4D7F3161BBB22E500AAB0C0 /* Hash.swift */,
D45A36C81BBC667D00BE3DDE /* Category.swift */,
@ -243,7 +238,6 @@
children = (
D432D4701BA9AC0B00F3FABC /* DiffTests.swift */,
D40024ED1BAC819000A110B8 /* SwiftTests.swift */,
D4A71DC61BB4AC9E0051416D /* VertexTests.swift */,
D4AAE5141B5AE22E004E581F /* Info.plist */,
D40B89C71BC439000078E098 /* Assertions.framework */,
);
@ -404,7 +398,6 @@
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */,
D49FCBC21BBEF2C600C5E9C3 /* Fix.swift in Sources */,
D49FCBC41BBEF98E00C5E9C3 /* Free.swift in Sources */,
D4A71DC51BB45B850051416D /* Vertex.swift in Sources */,
D4AAE54A1B5AE2D0004E581F /* Syntax.swift in Sources */,
D432D4731BA9C55300F3FABC /* Stream.swift in Sources */,
D49FCBC61BBF214300C5E9C3 /* Patch.swift in Sources */,
@ -428,7 +421,6 @@
files = (
D40024EE1BAC819000A110B8 /* SwiftTests.swift in Sources */,
D432D4711BA9AC0B00F3FABC /* DiffTests.swift in Sources */,
D4A71DC71BB4AC9E0051416D /* VertexTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -1,127 +0,0 @@
public enum Vertex<Element>: CustomDebugStringConvertible, CustomStringConvertible {
indirect case XY(Element, Memo<Vertex>, Memo<Vertex>)
case End
public func analysis<Result>(@noescape ifXY ifXY: (Element, Memo<Vertex>, Memo<Vertex>) -> Result, @noescape ifEnd: () -> Result) -> Result {
switch self {
case let .XY(a, x, y):
return ifXY(a, x, y)
case .End:
return ifEnd()
}
}
public var isEmpty: Bool {
return analysis(ifXY: const(false), ifEnd: const(true))
}
public var element: Element? {
return analysis(
ifXY: { x, _, _ in x },
ifEnd: const(nil))
}
public var right: Memo<Vertex> {
return analysis(
ifXY: { _, xs, _ in xs },
ifEnd: const(Memo(evaluated: .End)))
}
public var down: Memo<Vertex> {
return analysis(
ifXY: { _, _, ys in ys },
ifEnd: const(Memo(evaluated: .End)))
}
public var diagonal: Memo<Vertex> {
return right.flatMap { $0.down }
}
public var row: Stream<Element> {
return Stream.unfold(Memo(evaluated: self)) {
$0.value.analysis(
ifXY: { here, across, _ in .Some(here, across) },
ifEnd: const(nil))
}
}
public var column: Stream<Element> {
return Stream.unfold(Memo(evaluated: self)) {
$0.value.analysis(
ifXY: { here, _, down in .Some(here, down) },
ifEnd: const(nil))
}
}
public var rowMajor: Stream<Stream<Element>> {
return Stream.unfold(Memo(evaluated: self), { (state: Memo<Vertex>) -> (Stream<Element>, Memo<Vertex>)? in
state.value.analysis(
ifXY: { _, _, down in (state.value.row, down) },
ifEnd: const(nil))
})
}
public var columnMajor: Stream<Stream<Element>> {
return Stream.unfold(Memo(evaluated: self), { (state: Memo<Vertex>) -> (Stream<Element>, Memo<Vertex>)? in
state.value.analysis(
ifXY: { _, across, _ in (state.value.column, across) },
ifEnd: const(nil))
})
}
public init<S1: SequenceType, S2: SequenceType>(rows: S1, columns: S2, combine: (S1.Generator.Element, S2.Generator.Element) -> Element) {
let rows = Stream(sequence: rows)
let columns = Stream(sequence: columns)
func across(vertex: Vertex<Element>) -> Stream<Memo<Vertex<Element>>> {
return Stream.unfold(Memo(evaluated: vertex)) { vertex in
vertex.value.analysis(
ifXY: { _, xs, _ in .Some(vertex, xs) },
ifEnd: const(.Some(vertex, vertex)))
}
}
self = rows
.map { a in columns.map { b in combine(a, b) } }
.fold(.End) {
$0.zipWith(across($1.value)).fold(.End) {
.XY($0.0, $1, $0.1)
}
}
}
public func map<Other>(transform: Element -> Other) -> Vertex<Other> {
switch self {
case let .XY(xy, xs, ys):
return .XY(transform(xy), xs.map { $0.map(transform) }, ys.map { $0.map(transform) })
case .End:
return .End
}
}
public var debugDescription: String {
return rowMajor.map {
$0.map { String(reflecting: $0) }.joinWithSeparator("\t")
}.joinWithSeparator("\n")
}
public var description: String {
return rowMajor.map {
$0.map { String($0) }.joinWithSeparator("\t")
}.joinWithSeparator("\n")
}
}
public func == <A: Equatable> (left: Vertex<A>, right: Vertex<A>) -> Bool {
switch (left, right) {
case (.End, .End):
return true
case let (.XY(a, x1, y1), .XY(b, x2, y2)):
return a == b && x1.value == x2.value && y1.value == y2.value
default:
return false
}
}

View File

@ -1,47 +0,0 @@
final class VertexTests: XCTestCase {
func testConstructingWithStreamsProducesMatrixOfProducts() {
let actual = Vertex(rows: Stream(sequence: [ "a", "b" ]), columns: Stream(sequence: [ "0", "1" ]), combine: +)
let expected: Vertex<String> = {
let end = Memo<Vertex<String>>(evaluated: .End)
let b1 = Memo<Vertex<String>> { .XY("b1", end, end) }
let b0 = Memo<Vertex<String>> { .XY("b0", b1, end) }
let a1 = Memo<Vertex<String>> { .XY("a1", end, b1) }
let a0 = Memo<Vertex<String>> { .XY("a0", a1, b0) }
return a0.value
}()
XCTAssert(actual == expected, "\(String(reflecting: actual)) != \(String(reflecting: expected))")
}
func testConstructingWithStreamsProducesMatrix() {
let actual = Vertex(rows: Stream(sequence: [ "a", "b", "c" ]), columns: Stream(sequence: [ "0", "1", "2" ]), combine: +)
let expected: Vertex<String> = {
let end = Memo<Vertex<String>>(evaluated: .End)
let c2 = Memo<Vertex<String>> { .XY("c2", end, end) }
let c1 = Memo<Vertex<String>> { .XY("c1", c2, end) }
let c0 = Memo<Vertex<String>> { .XY("c0", c1, end) }
let b2 = Memo<Vertex<String>> { .XY("b2", end, c2) }
let b1 = Memo<Vertex<String>> { .XY("b1", b2, c1) }
let b0 = Memo<Vertex<String>> { .XY("b0", b1, c0) }
let a2 = Memo<Vertex<String>> { .XY("a2", end, b2) }
let a1 = Memo<Vertex<String>> { .XY("a1", a2, b1) }
let a0 = Memo<Vertex<String>> { .XY("a0", a1, b0) }
return a0.value
}()
XCTAssert(actual == expected, "\(String(reflecting: actual)) != \(String(reflecting: expected))")
}
func testFib() {
XCTAssertEqual(Array(fib.take(7)), [ 1, 1, 2, 3, 5, 8, 13 ])
}
}
private let fib: Stream<Int> = Stream.unfold((0, 1)) { x, y in
(y, (y, x + y))
}
@testable import Doubt
import XCTest