Skip to content

Instantly share code, notes, and snippets.

View michaeldiamant's full-sized avatar

Michael Diamant michaeldiamant

View GitHub Profile
@michaeldiamant
michaeldiamant / dependentTypeEvents.scala
Created January 10, 2016 19:56
Use dependent types to represent a relationship between a command and its possible failures and successful outcomes (i.e. events). This representation encodes the relationship into the data types rather than a service-like interface.
object E1 {
// Encoding a 1:1 command to event example
sealed trait Event
case class E1(i: Int) extends Event
case class E2() extends Event
sealed trait Intent[Z <: Event] {
type Out = Z
}
case class C1(id: Int) extends Intent[E1]
case class C2(id: Int) extends Intent[E1]
@michaeldiamant
michaeldiamant / gist:0cbab8984b6c84daa9c7
Created December 10, 2015 15:09
Ansible unable to check for undefined vars using with_items
[michael@dca-infra01 ansible]$ cat /home/michael/ntoggle/ansible/roles/ntoggle.helix-service/tasks/required_vars.yml - name: check required variables are defined
fail: msg="Variable '{{ item }}' is undefined"
when: item is not defined
with_items:
- foo
- bar
- fail: msg="foo"
when: foo is not defined
@michaeldiamant
michaeldiamant / gist:3849746
Created October 7, 2012 21:55
Tagged types representing units of price
trait Usd
trait Eur
trait ProfitCalculator[T] {
def calculateProfit(executionPrice: BigDecimal @@ T, requestedPrice: BigDecimal @@ T): BigDecimal @@ T =
tag(executionPrice - requestedPrice)
}
@michaeldiamant
michaeldiamant / gist:3849665
Created October 7, 2012 21:31
Represent a price with a case class
case class UsdPrice(value: BigDecimal) {
def +(p: UsdPrice) = UsdPrice(value + p.value)
def -(p: UsdPrice) = UsdPrice(value - p.value)
}
case class EurPrice(value: BigDecimal) {
def +(p: EurPrice) = EurPrice(value + p.value)
@michaeldiamant
michaeldiamant / gist:2892756
Created June 8, 2012 00:56
Extensible trade execution processor
trait ExtensibleTradeExecutionProcessor extends TradeExecutionProcessor {
val requestedTradeRepository: RequestedTradeRepository
val priceMutators: Set[(RequestedTrade, BigDecimal) => BigDecimal]
def process(requestId: String, executionPrice: BigDecimal): Option[ExecutedTrade] =
for {
requestedTrade <- requestedTradeRepository.findById(requestId)
reportedPrice <- priceMutators.foldLeft(executionPrice)((price, mutator) => mutator(requestedTrade, price)).some
} yield
@michaeldiamant
michaeldiamant / gist:2892477
Created June 7, 2012 23:52
Complecting trade execution processor
trait ComplectingTradeExecutionProcessor extends TradeExecutionProcessor with MarkupApplicator {
val requestedTradeRepository: RequestedTradeRepository
val markingUpSymbol: String
val symbolMarkup: BigDecimal
val markupVolumeThreshold: Long
val volumeMarkup: BigDecimal
def process(requestId: String, executionPrice: BigDecimal): Option[ExecutedTrade] =
requestedTradeRepository.findById(requestId)
@michaeldiamant
michaeldiamant / gist:2563818
Created May 1, 2012 00:04
Monadic style order resolution
trait MonadicStyleNewOrderResolver extends NewOrderResolutionErrors {
val symbolDao: SymbolDao
val marketTakerDao: MarketTakerDao
val marketMakerDao: MarketMakerDao
def resolve(order: NewOrder): Validation[String, ResolvedNewOrder] =
for {
symbol <- symbolDao.findByName(order.symbolName)
.toSuccess(UnknownSymbol)
@michaeldiamant
michaeldiamant / gist:2552179
Created April 29, 2012 17:42
Java style order resolution
trait JavaStyleNewOrderResolver extends NewOrderResolutionErrors {
val symbolDao: SymbolDao
val marketTakerDao: MarketTakerDao
val marketMakerDao: MarketMakerDao
def resolve(order: NewOrder): Either[String, ResolvedNewOrder] = {
val symbol = symbolDao.findByName(order.symbolName)
if (symbol.isEmpty) {
return Left(UnknownSymbol)