|
| 1 | +// RUN: not %target-swift-frontend %s -parse |
| 2 | + |
| 3 | +/// Abstraction of numeric types that approximate real numbers |
| 4 | +protocol ApproximateRealType { |
| 5 | + init() // zero |
| 6 | + func * (Self,Self) -> Self |
| 7 | + func + (Self,Self) -> Self |
| 8 | + func / (Self,Self) -> Self |
| 9 | + func - (Self,Self) -> Self |
| 10 | + prefix func + (Self) -> Self |
| 11 | + prefix func - (Self) -> Self |
| 12 | +} |
| 13 | + |
| 14 | +extension Double : ApproximateRealType {} |
| 15 | +extension Float : ApproximateRealType {} |
| 16 | + |
| 17 | +// Abstraction of a mathematical vector |
| 18 | +protocol VectorType { |
| 19 | + init() |
| 20 | + typealias Element : ApproximateRealType |
| 21 | + func dotProduct(Self) -> Element |
| 22 | + |
| 23 | + |
| 24 | + // Extras |
| 25 | + var count: Int {get} |
| 26 | + subscript(Int) -> Element {get set} |
| 27 | + typealias Tail |
| 28 | +} |
| 29 | + |
| 30 | +struct EmptyVector<T: ApproximateRealType> : VectorType { |
| 31 | + init() {} |
| 32 | + typealias Element = T |
| 33 | + func dotProduct(other: EmptyVector) -> Element { |
| 34 | + return Element() // zero |
| 35 | + } |
| 36 | + var count: Int { return 0 } |
| 37 | + |
| 38 | + subscript(i: Int) -> Element { |
| 39 | + get { fatalError("subscript out-of-range") } |
| 40 | + set { fatalError("subscript out-of-range") } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct Vector<Tail: VectorType> : VectorType { |
| 45 | + typealias Element = Tail.Element |
| 46 | + |
| 47 | + init(head: Element, tail: Tail) { |
| 48 | + self.head = head |
| 49 | + self.tail = tail |
| 50 | + } |
| 51 | + |
| 52 | + init() { |
| 53 | + self.head = Element() |
| 54 | + self.tail = Tail() |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + init(scalar: Element) { |
| 59 | + self.head = Element() |
| 60 | + self.tail = Tail() |
| 61 | + } |
| 62 | + */ |
| 63 | + |
| 64 | + func dotProduct(other: Vector) -> Element { |
| 65 | + return head * other.head + tail.dotProduct(other.tail) |
| 66 | + } |
| 67 | + |
| 68 | + var count: Int { return tail.count + 1 } |
| 69 | + var head: Element |
| 70 | + var tail: Tail |
| 71 | + |
| 72 | + subscript(i: Int) -> Element { |
| 73 | + get { return i == 0 ? head : tail[i - 1] } |
| 74 | + set { if i == 0 { head = newValue } else { tail[i - 1] = newValue } } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension VectorType where Tail == EmptyVector<Element> { |
| 79 | + init(_ scalar: Element) { |
| 80 | + self.init() |
| 81 | + self[0] = scalar |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +//===--- A nice operator for composing vectors ----------------------------===// |
| 86 | +//===--- there's probably a more appropriate symbol -----------------------===// |
| 87 | +infix operator ⋮ { |
| 88 | + associativity right |
| 89 | + precedence 1 |
| 90 | +} |
| 91 | + |
| 92 | +func ⋮ <T: ApproximateRealType> (lhs: T, rhs: T) -> Vector<Vector<EmptyVector<T> > > { |
| 93 | + return Vector(head: lhs, tail: Vector(head: rhs, tail: EmptyVector())) |
| 94 | +} |
| 95 | +func ⋮ <T: ApproximateRealType, U where U.Element == T> (lhs: T, rhs: Vector<U>) -> Vector<Vector<U> > { |
| 96 | + return Vector(head: lhs, tail: rhs) |
| 97 | +} |
| 98 | + |
| 99 | +extension Vector : CustomDebugStringConvertible { |
| 100 | + var debugDescription: String { |
| 101 | + if count == 1 { |
| 102 | + return "Vector(\(String(reflecting: head)))" |
| 103 | + } |
| 104 | + return "\(String(reflecting: head)) ⋮ " + (count == 2 ? String(reflecting: self[1]) : String(reflecting: tail)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +//===--- Test -------------------------------------------------------------===// |
| 109 | +print(Vector(head: 3.0, tail: EmptyVector())) |
| 110 | +print(3.0 ⋮ 4.0 ⋮ 5.0) |
| 111 | +print( (3.0 ⋮ 4.0 ⋮ 5.0).dotProduct(6.0 ⋮ 7.0 ⋮ 8.0) ) // 86.0 |
| 112 | + |
| 113 | +// print( (3.0 ⋮ 4.0 ⋮ 5.0).dotProduct(6.0 ⋮ 7.0) ) // Won't compile |
| 114 | + |
0 commit comments