This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Double: Multiplicative { | |
static var multId: Double { return 1.0 } | |
} | |
extension Double: Additive { | |
static var addId: Double { | |
return 0.0 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let doubleVect = Vector(fst: 1.0, snd: 1.0) | |
doubleVect.magnitude |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Complex: Equatable { | |
let real: Double | |
let imm: Double | |
} | |
extension Complex: CustomStringConvertible { | |
var description: String { return "\(real) + \(imm)i" } | |
} | |
extension Complex: TwoDimensions { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let i = Complex(real: 0.0, imm: 1.0) | |
let one = Complex(real: 1.0, imm: 0.0) | |
let other = Complex(real: Double.pi, imm: sqrt(2.0)) | |
i * i == (-1) ◊ one | |
Complex.multId * i == i | |
other.magnitude | |
17.3 ◊ other |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Linear<A> { | |
let x: A | |
let c: A // c stands for 'constant', it's the constant factor to the linear function | |
} | |
extension Linear: CustomStringConvertible where A: CustomStringConvertible { | |
var description: String { return "f(x) = \(x)x + \(c)" } | |
} | |
extension Linear: Equatable where A: Equatable {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let constOne = Linear(x: 0, c: 1) | |
let constThree = Linear(x: 0, c: 3) | |
let verySteep = Linear(x: 200, c: -5) | |
let moveUpByThree = { $0 + constThree } | |
constOne + Linear.addId == constOne | |
moveUpByThree(constOne) | |
285 ◊ verySteep |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Vector: FancyMult where A: Additive, A: Multiplicative { | |
typealias FancyVal = A | |
static func ** (lhs: Vector<A>, rhs: Vector<A>) -> A { | |
return lhs.fst * rhs.fst + lhs.fst * rhs.fst | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Quadratic<A> { | |
let x²: A | |
let x: A | |
let c: A | |
} | |
extension Quadratic: Equatable where A: Equatable {} | |
extension Linear: FancyMult where A: Additive, A: Multiplicative { | |
typealias FancyVal = Quadratic<A> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator **: MultiplicationPrecedence | |
protocol FancyMult { | |
associatedtype FancyVal | |
static func ** (lhs: Self, rhs: Self) -> FancyVal | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let linearMinus = Linear(x: 3, c: -5) | |
let linearPlus = Linear(x: 3, c: 5) | |
let quadMinus = Quadratic(x²: 9, x: 0, c: -25) | |
linearPlus ** linearMinus == quadMinus |