Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka)....

12
Helsinki Scala Club 27.3.2013

Transcript of Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka)....

Page 1: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

Helsinki Scala Club27.3.2013

Page 2: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

Summary :)• Multi-paradigm, strongly typed JVM language

• Runs on JVM (good Java interoperability)

• Exotic build tools (sbt)

• Very lean syntax

• Sometimes too _

• Open source, backed by Typesafe

• Awesome for scaling up & out (w/ Akka)

• Complex to master - easy to dip

Page 3: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Multi-paradigm= many ways to do a thing

Opposed to i.e. Python’s ”one obvious (and best) way”No one thinking is by itself considered ”better” than the others.

Immutable data structures often preferred, since they scale better and fit functional thinking.But you can use Scala simply as a ”better Java” (please don’t).

Object oriented

Strongly typed

Scripting languages

Functional

Page 4: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Runs on JVMMultiple JVM implementations, including 100% open source OpenJVM.

Existing, well performing (and documented) platform.

No need to reinvent primitives s.a. OS access, networking, basic multithreading.

Up to C++ level performance, if HotSpot™ kicks in.

Some language features (s.a. short, float, null) are there only for Java compatibility.

Also other implementations (.NET, LLVM) are possible.

Strong push because of existing Java libraries.

Page 5: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Exotic build tools

sbt = Scala Built Tool (NOT ‘simple build tool’!)

”Does it all” attitude: handles apt-get and make functionality.

Build scripts either .sbt or .scala.

+ sniffs dependencies between source code automatically (like C#)+ fairly easy to get started- complex to master- expects a certain directory structure (Scala itself doesn’t)- slooooooooow (well, compiling Scala is!)

DEMO

Page 6: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Very lean syntax

Remember the charts? 3x less code than Java/C++

case class Point2d( x: Double, y: Double )

That’s a class.

def +( v: Vector2d )= new Point2d( x+v.dx, y+v.dy )

That’s a method.

That’s a closure.

override def equals(o: Any): Boolean = o match { case oo: Point2d => (oo-this).norm < EPSILON case _ => false }

That’s a method with multiple entry points (kind of).

Page 7: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Very lean syntax (cont’d)

List(41, "cat") collect { case i: Int => i + 1 }

That’s a ”partial function” with only entry for ints.

List(-12, 41) collect { case i if i<0: Int => i + 1 }

That’s a partial function with only entry for negative ints.

More on partial functions: http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html

BACK TO TRACK

Page 8: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Sometimes too _

One can do this:

List(1,2,3) map (_+2)

Definitely not this:

List((1,2),(3,4)) map (_._1)

But this is better:

List(1,2,3) map (x => x+2)

Perhaps better:

List((1,2),(3,4)) map { case (a,_) => a }

There are N uses for the poor ’_’ in Scala:

- don’t use it as a variable name postfix (makes a ”partially applied function” aka C++ function pointer)

- generally makes syntax browsing and compiler error messages awkward, at places

- for most use there is a better (slightly longer, but more readable) alternative

More info: www.slideshare.net/normation/scala-dreaded

In Scala 2.10 some of the language features have become optional: they must be switched on by each source needing them, or by a global flag.

Note: this is a genuinlyokay use for ’_’

Page 9: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Open Source, backed by

”The languages that are properly financed will stick around.”

- Martin Odersky (I think...)

They also have (paying) customers:

full list of customers

Scala itself is Open Source (BSD-like license)

JVM itself is available as an open source implementation (OpenJDK)

They have more than just the language:

- Akka, ”event driven middleware for Scala and Java”

- Typesafe Console, ”an enterprise-grade dashboard for monitoring your application”

Page 10: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Awesome for scaling

Akka is like Erlang programming model on JVM (naming after a Swedish mountain maybe not a coincidence?).

- Scales up = fills all your cores- Scales out = fills your cluster

Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

Calxeda coming out with 1000 servers in a rack. Akka gives you means to tap into them, seamlessly.

Manages grouping agents to resettable groups, etc. Manages failure and recovery. The Akka book is as thick as the Scala book.

Page 11: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

What Scala? Easy to dipScala for Java Refugees = one doesn’t have to eat the whole elephant up front!

- Allows coding as if ”a better Java”- Dip by creating one simple, practical piece in Scala first (remember: full Java integration!)- Twitter etc. have good summaries of their transition (pros/cons)

- ”Getting over Java” (or C++) may be the most challenging part = do eventually learn the map, match etc. i.e. think in Scala.- Getting *back* to Java is painful! :)

Complex to masterScala essentially packs C++, Scheme and a scripting language in ONE. :)”Many ways” is always more complex than ”one obvious way”

Good language for consultants!? :D

the end

Page 12: Helsinki Scala Club 27.3 270313.pdf · Scala had multiple Actor libraries (Scala 2.9, Lift, Akka). Since 2.10 Akka seems to be the norm (slightly more complex, but without limits).

Next?• Hardware accelerated JVM-based GUI framework

• Part of Java 7 SE

• Very suited to alternative JVM languages

• DEMO: own code

• Scene tree benefits (like JavaScript, but type safe)

• Ready to touch

• Markets - automotive, embedded, Fat Client

• Hardware and OS requirementsHomework:

test out JavaFX Ensemble