Skip to content

Instantly share code, notes, and snippets.

/Vector.swift Secret

Created September 20, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/790a690597069fe70b8c874a042d52d0 to your computer and use it in GitHub Desktop.
Save anonymous/790a690597069fe70b8c874a042d52d0 to your computer and use it in GitHub Desktop.
Simple example using SwiftInSwift ( https://gist.github.com/anonymous/07d9df1a80820bb5abf5a2c671fd223f ), and some definitions of stuff not visible here, ie the +=| and «» operators, etc.
// DEF-{
func _sisCodeForVectorProtocols() -> [String] {
var lines = [String]()
for c in 2 ... 4 {
lines +=| "public protocol Vector\(c) : CustomStringConvertible {"
lines +=| " associatedtype Element"
lines +=| " var elements: («Element») { get set }" «» c
lines +=| " init(«_ §a: Element»)" «» c
lines +=| "}"
}
for c in 2 ... 4 {
lines +=| "public extension Vector\(c) {"
lines +=| " init(all value: Element) { self.init(«value») }" «» c
lines +=| " var description: String { return String(describing: elements) }"
lines +=| " var §a: Element { return elements.§N }" *| c
lines +=| "}"
}
return lines
}
// }-DEF
// PRINT-{ _sisCodeForVectorProtocols()
public protocol Vector2 : CustomStringConvertible {
associatedtype Element
var elements: (Element, Element) { get set }
init(_ a: Element, _ b: Element)
}
public protocol Vector3 : CustomStringConvertible {
associatedtype Element
var elements: (Element, Element, Element) { get set }
init(_ a: Element, _ b: Element, _ c: Element)
}
public protocol Vector4 : CustomStringConvertible {
associatedtype Element
var elements: (Element, Element, Element, Element) { get set }
init(_ a: Element, _ b: Element, _ c: Element, _ d: Element)
}
public extension Vector2 {
init(all value: Element) { self.init(value, value) }
var description: String { return String(describing: elements) }
var a: Element { return elements.0 }
var b: Element { return elements.1 }
}
public extension Vector3 {
init(all value: Element) { self.init(value, value, value) }
var description: String { return String(describing: elements) }
var a: Element { return elements.0 }
var b: Element { return elements.1 }
var c: Element { return elements.2 }
}
public extension Vector4 {
init(all value: Element) { self.init(value, value, value, value) }
var description: String { return String(describing: elements) }
var a: Element { return elements.0 }
var b: Element { return elements.1 }
var c: Element { return elements.2 }
var d: Element { return elements.3 }
}
// }-PRINT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment