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.

Aki Saarinen

October 19, 2012
Tweet

More Decks by Aki Saarinen

Other Decks in Technology

Transcript

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

    one wants the computer to do” Donald Knuth
  2. Imperative while var return for  loop ... FP tail  recursion,

     tco val,  lazy  val expressions for  comprehension ...
  3. Syntactic features • Symbolic function names • Optional . ;

    () {} • Infix function calls (1  max  2) • Many uses for underscore (_) • ...
  4. while var return tail  recursion    over val,  lazy  val

         over expressions          over classes ADTs                        over methods functions              over
  5. • Only if there’s a known meaning • Mathematics and

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

    <& >\ >> >~ >- >>~ <> </> ># >| >>> >:> >+ ~> >+> >!
  7. • OO-style extension methods Implicit Conversions val  timeout  =  3

     seconds for  (i  <-­‐  1  until  10)  {  ..  }
  8. • 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))
  9. trait  Agent[State]  {    def  initialState:  State    def  actions(

                   state:  State,                  event:  Event):  (State,  Actions) } No side-effects
  10. 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) }
  11. 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    ) )
  12. 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 }
  13. • Our style: FP + KISS • Everyone should be

    opinionated • Define your style