Created
December 1, 2010 09:04
-
-
Save debasishg/723204 to your computer and use it in GitHub Desktop.
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
// 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