Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Opinionated Scala (Reaktor Dev Day 2012)

Opinionated Scala (Reaktor Dev Day 2012)

Valo Research and Trading develops real-time automated trading systems and large-scale data processing applications for the financial markets with Scala.

Scala has a diverse feature set and follows a multiparadigm approach, which makes deciding on design approach and coding style difficult. Scala code can be written in many ways and opinions on best practices vary. Mixing too many styles in a single codebase can result in confusing overall design, which is why Valo writes their Scala in a very specific, opinionated way.

Aki Saarinen discusses the experiences of developing a particular Scala design style, and how Valo ended up with their conventions. The aim of the talk is to challenge the all too typical “better Java” approach and give practical tips on using Scala.

2ec6bb11c8fcfa22a6f13545e5e836c5?s=128

Aki Saarinen

October 19, 2012
Tweet

More Decks by Aki Saarinen

Other Decks in Technology

Transcript

  1. Opinionated SCALA @AKISAARINEN VALO RESEARCH AND TRADING

  2. “A better Java” http://www.flickr.com/photos/powi/4059932439/

  3. None
  4. None
  5. http://www.flickr.com/photos/jeffyoungstrom/712961369/

  6. Everyone using Scala should be opinionated

  7. Outline • Our setup • Good programs • Scala pitfalls

    • Our Scala-style • Questions
  8. VALO Research and Trading

  9. http://www.flickr.com/photos/osiatynska/3287986172

  10. None
  11. Why Scala? • Productivity, adapting quickly • Good understanding of

    JVM • Tooling • Java-interoperability
  12. Speed? • Not ultra high-frequency • Surprisingly fast, though •

    Milliseconds, sub-milliseconds
  13. Good programs?

  14. Abstraction

  15. “Programming is the art of telling another human being what

    one wants the computer to do” Donald Knuth
  16. Simplifying

  17. SCALA Pitfalls

  18. Functional Object-oriented Imperative

  19. Imperative while var return for  loop ... FP tail  recursion,

     tco val,  lazy  val expressions for  comprehension ...
  20. OO FP abstract  data  types functions type  classes classes methods

    extension  methods ... ...
  21. Syntactic features • Symbolic function names • Optional . ;

    () {} • Infix function calls (1  max  2) • Many uses for underscore (_) • ...
  22. JVM limitations • Null • Type erasure • Bad tail-call-optimization

  23. No guidelines

  24. Our Scala-style

  25. Functional programming and immutability

  26. “Has-kell-ify” verb: improve the appearance of

  27. while var return tail  recursion    over val,  lazy  val

         over expressions          over classes ADTs                        over methods functions              over
  28. Maximize readability Simplify

  29. Examples

  30. • Only if there’s a known meaning • Mathematics and

    Theoretical CS • Linear algebra • Parser combinators (No) Symbolic names
  31. Dispatch, WTF?!? /\ :/ / <<? <<< << <:< as_!

    <& >\ >> >~ >- >>~ <> </> ># >| >>> >:> >+ ~> >+> >!
  32. Implicits

  33. • OO-style extension methods Implicit Conversions val  timeout  =  3

     seconds for  (i  <-­‐  1  until  10)  {  ..  }
  34. • Typeclasses: Implicit Parameters Numeric,  Ordering,  ... case  class  Test(i:

     Int) implicit  val  TestOrdering  =  new  Ordering[Test]  {    def  compare(a:  Test,  b:  Test)  =  a.i  -­‐  b.i } val  tests  =  List(Test(3),  Test(1),  Test(2)) tests.sorted res1:  List[Test]  =  List(Test(1),  Test(2),  Test(3))
  35. Implicits: the bad part http://www.flickr.com/photos/spacematters/2150854729/

  36. Agent is a pure function.

  37. trait  Agent[State]  {    def  initialState:  State    def  actions(

                   state:  State,                  event:  Event):  (State,  Actions) } No side-effects
  38. case  class  NokiaAgentState(holdings:  Long) Abstract data types

  39. object  NokiaAgent  extends  Agent[NokiaAgentState]  {    def  initialState  =  NokiaAgentState(holdings

     =  0)    def  actions(state:  NokiaAgentState,  event:  Event)  =  {        event  match  {            case  News(company)  if  company  ==  “NOK1V”  =>                  val  shares  =  100                val  newState  =  update(state,  shares)  val  target  =  Portfolio(“NOK1V”  -­‐>  newState.holdings)                (newState,  Actions.portfolioTransformation(target))            case  _  =>                (state,  Actions.none)        }    }    private  def  update(state:  NokiaAgentState,  shares:  Long)  =        state.copy(holdings  =  state.holdings  +  shares) }
  40. Simple DSLs with limited scope

  41. Filtering news with a DSL NewsFilter.where(    company    

     isFrom  (Country.US),    company      belongsTo  (Index.DJI),    category    contains  ("dividend"),    time            isBetween(        start  =  new  LocalTime(12,10),        end      =  new  LocalTime(13,30),        zone    =  TimeZone.EST    ) )
  42. Speed?

  43. http://www.flickr.com/photos/jeff-o-matic/2055538757

  44. def  parseMessage(buffer:  ByteBuffer)  =  {    if  (buffer.position  ==  buffer.limit)

           throw  new  PartialMessageException    val  msgType  =          messageType(buffer.get(buffer.position))    if  (buffer.limit  <  buffer.position  +  msgType.size)        throw  new  PartialMessageException    val  msg  =  msgType.parse(buffer.slice)    buffer.position(buffer.position  +  msgType.size)    msg }
  45. Summary

  46. http://www.flickr.com/photos/newbirth/295135405/ A chainsaw?

  47. • Our style: FP + KISS • Everyone should be

    opinionated • Define your style
  48. Thank you @AKISAARINEN http://valotrading.com http://www.github.com/valotrading http://blog.akisaarinen.fi