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
package com.ovoenergy | |
import java.util.{Calendar, Date} | |
object Aggregator { | |
case class DailySnapshot(date: Date, amount: Double) | |
case class MonthlyConsumption(date: Date, amount: Double) | |
def parseData(data: Seq[(String, Double)]): Seq[DailySnapshot] = { |
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
// 3²x² + 5² | |
let quadPlus = Quadratic(x²: 3 ^^ 2, x: 30, c: 5 ^^ 2) | |
linearPlus ** linearPlus == quadPlus |
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 ai = Complex(real: 18.9, imm: -19.3) | |
let bi = Complex(real: 4.4, imm: 0.2) | |
let vecti = Vector(fst: ai, snd: bi) | |
one ◊ vecti | |
vecti + vecti | |
vecti ** vecti |
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 vectj = Vector(fst: Complex.multId, snd: Complex.addId) | |
let fVecti = Linear(x: Vector.addId, c: vecti) | |
let fVectj = Linear(x: vectj, c: vectj) | |
fVecti + fVectj | |
Linear.addId + fVectj == fVectj |
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
operator ^^: PowerPrecedence | |
precedencegroup PowerPrecedence { | |
higherThan: MultiplicationPrecedence | |
associativity: right | |
} | |
func ^^ <A: Multiplicative> (lhs: A, rhs: Int) -> A { | |
var a = A.multId | |
for _ in 0..<rhs { | |
a = a * lhs |
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 |
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
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
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
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 |