Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created December 15, 2017 15: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 yasuabe/664faa0b975c912e1e6fc9af07a9b8ec to your computer and use it in GitHub Desktop.
Save yasuabe/664faa0b975c912e1e6fc9af07a9b8ec to your computer and use it in GitHub Desktop.
ch02 degenerate objects
package fp_tdd
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
object Chapter02 extends Properties("Ch02") {
// ======== TODO ========
// $5 + 10 CHF = $10 if rate is 2:1
// Make "amount" private
// Money rounding?
// ======== DONE ========
// 1. $5 * 2 = $10
// 2. Dollar side effect
// 2. times -> `*`
// product code -------------------------------------------------------------------
case class Dollar(amount: Int) {
def *(multiplier: Int): Dollar = Dollar(amount * multiplier)
}
// properties -------------------------------------------------------------------
property("$0 * x = $0") = forAll { (x: Int) =>
Dollar(0) * x == Dollar(0)
}
property("$1 * x = $x") = forAll { (x: Int) =>
Dollar(1) * x == Dollar(x)
}
property("$a * b = $b * a") = forAll { (a: Int, b: Int) =>
Dollar(a) * b == Dollar(b) * a
}
property("($a * b) * c = $a * (b * c)") = forAll { (a: Int, b: Int, c: Int) =>
(Dollar(a) * b) * c == Dollar(a) * (b * c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment