Created
February 25, 2015 03:22
-
-
Save dimitrisli/e85ee179e7f9358be5d1 to your computer and use it in GitHub Desktop.
ScalaForexRatesDSL
This file contains 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
object ScalaForexRatesDSL { | |
implicit class ccy2money(ccy :Symbol) { | |
def apply(amount: BigDecimal) = Money(amount, ccy) | |
} | |
} | |
case class FromToCcy(ccyFrom: Symbol, ccyTo: Symbol) | |
case class Money(amount:BigDecimal, ccy:Symbol) { | |
def + (other: Money)(implicit rates: Map[FromToCcy, BigDecimal]): Money = | |
if(ccy == other.ccy) Money(amount + other.amount, ccy) else { | |
val convertedOther = convertedOtherMoney(other) | |
Money(amount + convertedOther.amount, ccy) | |
} | |
def - (other: Money)(implicit rates: Map[FromToCcy, BigDecimal]): Money = | |
if(ccy == other.ccy) Money(amount - other.amount, ccy) else { | |
val convertedOther = convertedOtherMoney(other) | |
Money(amount - convertedOther.amount, ccy) | |
} | |
def * (other: Money)(implicit rates: Map[FromToCcy, BigDecimal]): Money = | |
if(ccy == other.ccy) Money(amount * other.amount, ccy) else { | |
val convertedOther = convertedOtherMoney(other) | |
Money(amount * convertedOther.amount, ccy) | |
} | |
def / (other: Money)(implicit rates: Map[FromToCcy, BigDecimal]): Money = | |
if(ccy == other.ccy) Money(amount / other.amount, ccy) else { | |
val convertedOther = convertedOtherMoney(other) | |
Money(amount / convertedOther.amount, ccy) | |
} | |
private def convertedOtherMoney(other: Money)(implicit rates: Map[FromToCcy, BigDecimal]): Money = { | |
val rate = rates.get(FromToCcy(other.ccy, ccy)).getOrElse(1 / rates.get(FromToCcy(ccy, other.ccy)).get) | |
Money(other.amount * rate, ccy) | |
} | |
def in(ccy: Symbol)(implicit rates: Map[FromToCcy, BigDecimal]): Money = | |
if (this.ccy == ccy) this else { | |
val rate = rates.get(FromToCcy(this.ccy, ccy)).getOrElse(1 / rates.get(FromToCcy(ccy, this.ccy)).get) | |
Money(amount * rate, ccy) | |
} | |
override def toString = amount + " " + ccy.name | |
} | |
object Main extends App { | |
import ScalaForexRatesDSL._ | |
implicit val rates = Map((FromToCcy('EUR, 'USD) -> BigDecimal(1.13))) | |
print( 'USD(100) + 'EUR(200) in 'EUR ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps the Money case class should receive the rates implicit, instead of each operator? It seems likely that you would like a consistent exchange rate