Skip to content

Instantly share code, notes, and snippets.

@andrevidela
Last active January 7, 2019 14:40
Show Gist options
  • Save andrevidela/192bb551bb80919f3961d6c15b718db3 to your computer and use it in GitHub Desktop.
Save andrevidela/192bb551bb80919f3961d6c15b718db3 to your computer and use it in GitHub Desktop.
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 {}
extension Linear: TwoDimensions {
typealias ComponentVal = A
static func make2D(_ fst: A, _ snd: A) -> Linear {
return Linear(x: fst, c: snd)
}
var fst: A { return x }
var snd: A { return c }
}
extension Linear: Additive where A: Additive {}
extension Linear: Scalar where A: Multiplicative { typealias ScalarVal = A }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment