Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created December 1, 2010 09:04
Show Gist options
  • Save debasishg/723204 to your computer and use it in GitHub Desktop.
Save debasishg/723204 to your computer and use it in GitHub Desktop.
// validate trade quantity
def validQuantity(qty: BigDecimal): Validation[String, BigDecimal] =
try {
if (qty <= 0) "qty must be > 0".fail
else if (qty > 500) "qty must be <= 500".fail
else qty.success
} catch {
case e => e.toString.fail
}
// validate unit price
def validUnitPrice(price: BigDecimal): Validation[String, BigDecimal] =
try {
if (price <= 0) "price must be > 0".fail
else if (price > 100) "price must be <= 100".fail
else price.success
} catch {
case e => e.toString.fail
}
// make a trade or report validation failures
def makeTrade(account: Account, instrument: Instrument, refNo: String, market: Market,
unitPrice: BigDecimal, quantity: BigDecimal) =
(validUnitPrice(unitPrice).liftFailNel |@|
validQuantity(quantity).liftFailNel) { (u, q) => Trade(account, instrument, refNo, market, u, q) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment