+ All Categories
Home > Technology > Scala In The Wild

Scala In The Wild

Date post: 10-May-2015
Category:
Upload: djspiewak
View: 3,238 times
Download: 2 times
Share this document with a friend
Popular Tags:
32
Scala in the Wild Daniel Spiewak
Transcript
Page 1: Scala In The Wild

Scala in the WildDaniel Spiewak

Page 2: Scala In The Wild

Vanity Slide

• Software Developer at Novell

• Working on a next-gen communications system called “Pulse”

• Author of Scala for Java Refugees

• ...and a fair bit more

• An unhealthy fascination with languages

Page 3: Scala In The Wild

Brief Overview of Pulse

• Next-Gen communications platform

• Implemented as a web application

• AJAX

• Comet

• Focus on security and enterprise management

Page 4: Scala In The Wild
Page 5: Scala In The Wild
Page 6: Scala In The Wild
Page 7: Scala In The Wild

Architecture

Webapp (Lift)

Search Service (SOLR)

Notification Service

HBase

RabbitMQ

MySQL+

WFP Service

Messaging Service

Page 8: Scala In The Wild

Nifty Tricks

• Messages are chunks of XML

Page 9: Scala In The Wild

<message id="42"> <line/>Playing with Pulse (and trying to avoid accidentally poluting any of Andy's demos).

<message id="43"> <line/>We can reply quite easily. </message></message>

Page 10: Scala In The Wild

Nifty Tricks

• Messages are chunks of XML

• Extend Elem

• Custom NoBindingFactoryAdapter

• Use #load(String)

Page 11: Scala In The Wild

Nifty Tricks

• Messages are chunks of XML

• All frontend services use Cake Pattern

• Very extensive use of actors

Page 12: Scala In The Wild

Challenges

• Actors require a recovery mechanism

• Java / Scala interop is a little clumsy

• Collections conversion (java-utils)

Page 13: Scala In The Wild

import scala.collection.jcl.Conversions._ def foo(bar: java.util.List[String]) = { val xs: Seq[String] = bar bar.add("baz") xs foreach println // does it contain "baz"?} def foo(bar: List[String]) = { val back = new java.util.ArrayList[String] bar foreach back.add back}

Page 14: Scala In The Wild

import org.scala_tools.javautils.Implicits._

def foo(bar: java.util.List[String]) = { val xs = bar.asScala bar.add("baz") xs foreach println}

// returns java.util.List[String], and in O(1) too!def foo(bar: List[String]) = bar.asJava

Page 15: Scala In The Wild

Challenges

• Actors require a recovery mechanism

• Java / Scala interop is a little clumsy

• Collections conversion (java-utils)

• Spring doesn’t like mixins

• Tools are primitive (at best)

• Scala devs are in short supply

Page 16: Scala In The Wild

Java Refugees

• Tendency to write Java in Scala

Page 17: Scala In The Wild

class Person(fn :String, ln :String) { private var theFirstName :String = fn; private var theLastName :String = ln; def getFirstName() :String = { return theFirstName; } def setFirstName(fn :String) :Unit = { theFirstName = fn; } // ... override def equals(obj: Any) :Boolean = { if (obj.isInstanceOf[Person]) { var p :Person = obj.asInstanceOf[Person]; return p.getFirstName() == theFirstName && p.getLastName() == theLastName; } return false; }}

Page 18: Scala In The Wild

class Person(var firstName: String, var lastName: String) { override def equals(a: Any) = a match { case that: Person => { this.firstName == that.firstName && this.lastName == that.lastName } case _ => false }}

Page 19: Scala In The Wild

Java Refugees

• Tendency to write Java in Scala

• Failure to exploit advanced features

• Nested imports

• Point-Free / Currying

• Monads

Page 20: Scala In The Wild

def foo(bar: Bar) = { var result: Baz = null Box.!!(bar).foreach(person => Box.!!(person.name).foreach(name => Box.!!(name.find("blah")).foreach(result = _))) Box !! result}

Page 21: Scala In The Wild

def foo(bar: Bar) = { for { person <- Box !! bar name <- Box !! person.name result <- Box !! (name find "blah") } yield result}

Page 22: Scala In The Wild

Java Refugees

• Tendency to write Java in Scala

• Failure to exploit advanced features

• Nested imports

• Point-Free / Currying

• Monads

• Higher-Kinds only raise confusion

• Arbitrary file organization

Page 23: Scala In The Wild

Solution: Conventions!

http://davetron5000.github.com/scala-style/

Page 24: Scala In The Wild

Scala Style• Inspirations

• Java

• Standard ML

• Haskell

• C#

• OCaml

• Ruby

• Python

Page 25: Scala In The Wild

Scala Style

• Leverage type inference!

val s: String = "blah" // wrongval s = "blah" // right

Page 26: Scala In The Wild

Scala Style

• Leverage type inference!

• Higher-Order functions// wrongdef foldLeft[A](init: A, f: (A, B) => A) = ...// rightdef foldLeft[A](init: A)(f: (A, B) => A) = ...

foldLeft(0) { (a, b) => ... }

Page 27: Scala In The Wild

Scala Style

• Leverage type inference!

• Higher-Order functions

• Function valuesfoo (x) => x + 1 // wrongfoo (x) => { x + 1 } // wrong

foo { _ + 1 } // rightfoo { x => x + 1 } // right

Page 28: Scala In The Wild

Scala Style

• Leverage type inference!

• Higher-Order functions

• Function values

• Correct use of implicits

• Pimp-my-library

• Correcting inheritance (typeclasses)

Page 29: Scala In The Wild

Scala Style

• ...

• Function values

• Rules for correct use of implicits

• Arity-0 methodsprintln // wrongprintln() // right

xs.length() // wrongxs.length // right

Page 30: Scala In The Wild

Scala Style

• Leverage type inference!

• Higher-Order functions

• Function values

• Rules for correct use of implicits

• Arity-0 methods

• File organization

Page 31: Scala In The Wild

Conclusion(s)

• Yes, Scala is ready for the enterprise!

• ...but there’s definitely room to improve

• Process and conventions are critical

• Community is developing standards. Participate!

Page 32: Scala In The Wild

Thank You


Recommended