Created
July 31, 2016 18:30
-
-
Save nmcb/38ea134181036721ee3924045d724d81 to your computer and use it in GitHub Desktop.
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
/** protobuf:marco:confirmative:6 */ | |
/** floating point operations in base 2 are incomparable */ | |
0.1 + 0.1 == 0.2 | |
0.1 + 0.2 == 0.3 // true ? | |
0.2 + 0.2 == 0.4 | |
/** floating point operations in base 10 are comparable */ | |
type Amount = BigDecimal | |
val Amount = BigDecimal | |
Amount(0.1) + Amount(0.1) == Amount(0.2) | |
Amount(0.1) + Amount(0.2) == Amount(0.3) // true ? | |
Amount(0.2) + Amount(0.2) == Amount(0.4) | |
/** lets validate if we can see if two lists are balanced */ | |
(0.05 + 0.03 + 0.02) == (0.10) // sum = 0.10 | |
(0.05 + 0.13 + 0.02) == (0.13, 0.07) // sum = 0.20, true ? | |
(0.05 + 0.13 + 0.12) == (0.13, 0.17) // sum = 0.30 | |
/** returns true iff the sum of debits balances the sum of credits */ | |
def balanced(debits: Seq[Amount], credits: Seq[Amount]) = debits.sum == credits.sum | |
balanced(Seq(0.05, 0.03, 0.02), Seq(0.10)) // sum = 0.10 | |
balanced(Seq(0.05, 0.13, 0.02), Seq(0.13, 0.07)) // sum = 0.20, true ? | |
balanced(Seq(0.05, 0.13, 0.12), Seq(0.13, 0.17)) // sum = 0.30 | |
/** lets prototype the domain */ | |
trait Journal | |
case object Assets extends Journal | |
case object Liabilities extends Journal | |
case object Equities extends Journal | |
case object Revenues extends Journal | |
case object Expenses extends Journal | |
trait Side | |
case object Debit extends Side | |
case object Credit extends Side | |
trait Bookable { | |
def amount(side: Side): Amount | |
def debit = amount(Debit) | |
def credit = amount(Credit) | |
} | |
case class Line (journal: Journal, amount: Amount, side: Side) { | |
require(journal != null && amount != null && side != null, "must be sane") | |
require(amount > 0, "must be non-zero, positive") | |
} | |
case class Entry (lines: Seq[Line]) extends Bookable { | |
val journals = lines.map(_.journal) | |
require(debit == credit, "must be balanced") | |
require(journals.size == journals.toSet.size, "may contain onle line per journal") | |
override def amount(side: Side) = lines.filter(_.side == side).map(_.amount).sum | |
} | |
/** and model a balancing function */ | |
def balanceWith(entry: Entry, journal: Journal) = ??? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment