Skip to content

Instantly share code, notes, and snippets.

View debasishg's full-sized avatar
🏠
Working from home

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile
// 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))
// 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))
scala> import sbinary._
import sbinary._
scala> import sbinary.Operations._
import sbinary.Operations._
scala> import sbinary.DefaultProtocol._
import sbinary.DefaultProtocol._
scala> toByteArray[Long](1291224466794l)
@debasishg
debasishg / gist:729080
Created December 5, 2010 13:32
ascii artwork for arrows
// 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
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;}
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._
// 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")
@debasishg
debasishg / gist:757184
Created December 28, 2010 12:56
Sample use of a monad
-- example of a monadic operation
do s <- readFile f
writeFile g s
return s
-- gets translated to
readFile f >>= \s->
writeFile g s >>= \_->
@debasishg
debasishg / gist:757389
Created December 28, 2010 16:28
Monads .. again
val first = List(1, 2)
val next = List(8, 9)
for {
i <- first
j <- next
}
yield(i * j)
// gets converted to
@debasishg
debasishg / gist:758194
Created December 29, 2010 04:55
Monadic & Non monadic
// 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