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
// another trade | |
scala> val t2 = Map("account" -> "b-123", "instrument" -> "ibm", "refNo" -> "r-234", "market" -> "Singapore", "unitPrice" -> "15.25", "quantity" -> "400") | |
t2: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((quantity,400), (market,Singapore), (refNo,r-234), (account,b-123), (unitPrice,15.25), (instrument,ibm)) | |
scala> val trd2 = makeTrade(t2) | |
trd2: Option[net.debasishg.domain.trade.Trades.Trade] = Some(Trade(b-123,ibm,r-234,Singapore,15.25,400)) | |
scala> ((((List(trd1, trd2)) ∘∘ forTrade) ∘∘ taxFees) ∘∘ enrichWith) ∘∘ netAmount | |
res1: List[Option[scala.math.BigDecimal]] = List(Some(3307.5000), Some(8845.0000)) |
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
// failure case | |
scala> makeTrade("a-123", "google", "ref-12", Singapore, -10, 600) | |
res2: scalaz.Validation[scalaz.NonEmptyList[String],net.debasishg.domain.trade.Trades.Trade] = Failure(NonEmptyList(price must be > 0, qty must be <= 500)) | |
// success case | |
scala> makeTrade("a-123", "google", "ref-12", Singapore, 10, 200) | |
res3: scalaz.Validation[scalaz.NonEmptyList[String],net.debasishg.domain.trade.Trades.Trade] = Success(Trade(a-123,google,ref-12,Singapore,10,200)) |
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
scala> import sbinary._ | |
import sbinary._ | |
scala> import sbinary.Operations._ | |
import sbinary.Operations._ | |
scala> import sbinary.DefaultProtocol._ | |
import sbinary.DefaultProtocol._ | |
scala> toByteArray[Long](1291224466794l) |
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
// joining of 2 arrows | |
(>>>) :: arr a b -> arr b c -> arr a c | |
(>>>) | |
a -- (f) --> b -- (g) --> c | |
// works on pairs | |
// f: a -> b | |
// g: a -> c |
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
Intuitively, the (>=) :: m a → (a → m b) → m b of some Monad m allows the value returned by one computation to influence | |
the choice of another, whereas <*> the structure of a computation fixed, just sequencing the effects. | |
scala> def ev(i: Int) = {println("in ev"); if (i % 2 == 0) Some(i) else None;} | |
ev: (i: Int)Option[Int] | |
scala> def div5(i: Int) = {println("in div5"); if (i % 5 == 0) Some(i/5) else None;} | |
div5: (i: Int)Option[Int] | |
scala> def div3(i: Int) = {println("in div3"); if (i % 3 == 0) Some(i/3) else None;} |
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
scala> import net.debasishg.domain.trade.dsl._ | |
import net.debasishg.domain.trade.dsl._ | |
scala> import Trades._ | |
import Trades._ | |
scala> import scalaz._ | |
import scalaz._ | |
scala> import Scalaz._ |
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
// Reader monad | |
val lifecycle = for { | |
taxFeeIds <- forTrade // get the tax/fee ids for a trade | |
taxFeeValues <- taxFeeCalculate // calculate tax fee values | |
netAmount <- enrichTradeWith // enrich trade with net amount | |
} | |
yield((taxFeeIds ∘ taxFeeValues) ∘ netAmount) | |
// sample usage | |
val t1 = Map("account" -> "a-123", "instrument" -> "google", "refNo" -> "r-123", "market" -> "HongKong", "unitPrice" -> "12.25", "quantity" -> "200") |
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
-- example of a monadic operation | |
do s <- readFile f | |
writeFile g s | |
return s | |
-- gets translated to | |
readFile f >>= \s-> | |
writeFile g s >>= \_-> |
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
val first = List(1, 2) | |
val next = List(8, 9) | |
for { | |
i <- first | |
j <- next | |
} | |
yield(i * j) | |
// gets converted to |
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
// sample snippet that does not use monads | |
// note explicit null checks and imperative flow | |
String param(String key) { | |
//.. fetch value from request/session | |
return value; | |
} | |
Trade queryTrade(String ref) { | |
//.. query from db |