for some fun, I decided to start working on something that let me work with Money calculations. I realise there are libraries out there like Joda Money, but I'm doing this mainly for fun.
I was hoping to get a review on the way I've started to construct the library.
private def calculate(that: Money)(f: (BigDecimal, BigDecimal) => BigDecimal): Either[String, Money] = (this.currency, that.currency) match {
case (c, c1) if c == c1 => new Right(this.copy(this.currency, f(this.amount, that.amount)))
case _ => new Left(Money.COMPARE_ERROR_MESSAGE)
}
private def compare(that: Money)(f: (BigDecimal, BigDecimal) => Boolean): Either[String, Boolean] = (this.currency, that.currency) match {
case (c, c1) if c == c1 => new Right(this.amount > that.amount)
case _ => new Left(Money.COMPARE_ERROR_MESSAGE)
}
These two methods are incredibly similar except for the return type. Could someone suggest a way to improve this?