Securities trading is a domain on which I have been working since the last 10 years. I have implemented domain models of securities trading back office systems in Java and Scala. It's time to add scalaz to the mix and see how much more functional my model turns out to be. I have created a playground for this - tryscalaz is a repository on my github that hosts some of my experiments with scalaz. I have started building a domain model for trading systems. It's far from being a realistic one for production use - its main purpose is to make myself more conversant with scalaz.
Scalaz is a wonderful experiment - it's definitely what functional Scala programs should look like. It has a small but wonderful community - Jason (@retronym) and Runar (@runarorama) always help me proactively both on the mailing list and on Twitter.
I am not going into every detail of how my trade domain model shapes up with Scalaz. I implemented a similar domain model in Haskell very recently and documented it here, here and here on my blog. If nothing else, it will help you compare the various aspects of both the implementations.
In this post let me go through some of the features of Scalaz that I found wonderfully expressive to model your domain constraints. You can get a lot out of using Scala only. But with Scalaz, you can take your composition at a much higher level through the various combinators that it offers as part of implementing typeclasses for functors, applicatives, monads and arrows. I haven't yet explored all of these abstractions - yet many of those are already very useful in making your domain models concise, yet expressive.
Here's some example of composition using the higher order functions that Scalaz offers ..
Note how we can compose the functions much like the Haskell way that I described in the earlier posts. In the above composition, I used map, which we can do in Scala for lists or options which explicitly support a map operation that maps a function over the collection. With scalaz we can use mapping of a function over any
*->*for which there exists a
Functor[A]. Scala supports higher kinds and scalaz uses it to make
mapavailable more generally than what you get in the Scala standard library.
Now let's infuse some more Scalaz magic into it. Frequently we need to do the same operations on a list of trades, which means that instead of just a map, we need to lift the functions through another level of indirection. Much like ..
Note how the functions
taxFeesetc. get lifted into the List of Options.
Another nice feature that becomes extremely useful with scalaz in a domain model is the use of non-breaking error handling. This is made elegant by designing the
Validationabstraction as an applicative functor. You can design your validation functions of the domain model as returning an instance of
Validation. They can then be wired together in a variety of ways to implement accumulation of all failures before reporting to the user .. Here's a simple example from the
Tradedomain model ..
Validationin scalaz works much like Either, but has a more expressive interface that specifies the success and error types explicitly ..
You can use
Validationin comprehensions or as an applicative functor and wire up your domain validation logic in a completely functional way. Here's how our above validations work on the REPL ..
When we have invalid trade arguments all validation errors are accumulated and then reported to the user. If all arguments are valid, then we have a valid
Tradeinstance as success. Cool stuff .. a recipe that I would like to have as part of my domain modeling everytime I start a new project ..