Scala On Android

Post on 25-Jun-2015

1.096 views 1 download

Tags:

description

My presentation for Droidcon London, 2009

Transcript of Scala On Android

ANDROID AND SCALA: IS ANDROID READY FOR A SCALA INVASION?Presented by Akshay Dashrath

Which approach?

Performance

• Google Best Practices (http://developer.android.com/guide/practices/design/performance.html)

• NDK

For example…………

What's wrong with Java?

What about exploring alternative languages?

What about exploring alternative languages?

Abstraction Performance

Scala allows the programmer……

to write Clearer and more concise code

Scala is……

• Its Functional and OO• Statically typed• Supports type inference

• val file = new File("/sdcard/list.txt")

• Everything is an object• No primitives• x + 1 is the same as x .+(1)

• Supports Traits

So what's this “Trait”?• Similar to mixin• Nothing but an interface that supports concrete methods• Can “extend” multiple traits

For example…….• trait Similarity { def isSimilar(x: Any): Boolean def isNotSimilar(x: Any): Boolean = !isSimilar(x) }

• class Point(xc: Int, yc: Int) extends Similarity { var x: Int = xc var y: Int = yc def isSimilar(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x }

• object TraitsTest extends Application { val p1 = new Point(2, 3) val p2 = new Point(2, 4) val p3 = new Point(3, 3) println(p1.isNotSimilar(p2)) println(p1.isNotSimilar(p3)) println(p1.isNotSimilar(2)) }

Flexibility of Scala makes………• Its quite possible to develop internal DSL (Domain

Specific Languages)• Example

val name = “scala”

name contains “a” rather than name.contains(“a”)

• Wrappersclass ShouldWrapper(s: String) { def should = "should was invoked on " + s }

implicit def convert(s: String) = new ShouldWrapper(s)

“scala”.should

Output = java.lang.String = should was invoked on scala

Scala also………• Allows use of Scala as well as Java and Android APIs

seamlessly

A.scalaB.scala

A.classB.class

AB.dex AB.apkscalac dex zip

Scala is also designed with………• Message passing in mind

• Between threads

• So is Android• Between processes/applications

vs

• Java• public static LinkedList<RandomString> quicksort(LinkedList<RandomString> list) {

if (list.size() <= 1)return list;int pivot = list.size() / 2;LinkedList<RandomString> lesser = new LinkedList<RandomString>();LinkedList<RandomString> greater = new LinkedList<RandomString>();int sameAsPivot = 0;for (RandomString number : list) {if (number.getNum() > list.get(pivot).getNum())greater.add(number);else if (number.getNum() < list.get(pivot).getNum())lesser.add(number);elsesameAsPivot++;}lesser = quicksort(lesser);for (int i = 0; i < sameAsPivot; i++)lesser.add(list.get(pivot));greater = quicksort(greater);LinkedList<RandomString> sorted = new LinkedList<RandomString>();for (RandomString number : lesser)sorted.add(number);for (RandomString number: greater)sorted.add(number);return sorted;}}

vs

• Scala• def quicksort(l: List[RandomString]): List[RandomString] = {

l match {case List() => lcase _ => quicksort(for(x <- l.tail if x.num < l.head.num) yield x) :::

List(l.head) ::: quicksort(for(x <- l.tail if x.num >= l.head.num) yield x)}}

OR

• def quicksort(unsorted:List[RandomString]):List[RandomString] = { val len = unsorted.length if (len<2) unsorted else { val (left,right) = unsorted.splitAt(len/2) val pivot = right.head val rest = left ::: right.tail val (less,more) = rest.partition(_.num <= pivot.num) quicksort(less) ::: List(pivot) ::: quicksort(more) } }

And finally the shortcomings……• Performance on the Dalvik Virtual Machine

• Quick Sort of 100 integers and search for a string in Scala = 15.2 sec (java = 5.9 sec)

• Quick Sort method in Scala = 5.32 sec (Java = 1.051 sec)

• Memory• Lack of support• Learning curve

But Why?.........

• Boxing/Unboxing• Anonymous classes created for closures• Recursion?• Solution

• Scala 2.8• Dalvik JIT• ProGuard

Contact• akshaydashrath@gmail.com• www.akshaydashrath.com

Questions