Presentation is loading. Please wait.

Presentation is loading. Please wait.

An overview of Scala and Main Topics  Presentation intent.  What is Scala?  What is LiftWeb?  A trivial chat application  Q&A Author: Marius Danciu.

Similar presentations


Presentation on theme: "An overview of Scala and Main Topics  Presentation intent.  What is Scala?  What is LiftWeb?  A trivial chat application  Q&A Author: Marius Danciu."— Presentation transcript:

1 An overview of Scala and Main Topics  Presentation intent.  What is Scala?  What is LiftWeb?  A trivial chat application  Q&A Author: Marius Danciu

2 Presentation intent This document will not teach you Scala It barely scratches the Scala surface to have a slight idea of what Scala/LiftWeb is You decide if you like it or not. I think it REALLY worth …

3 What is Scala? Scala is a general purpose language, a hybrid OOP and Functional language invented by Martin Odersky from EPFL http://www.scala-lang.org/index.html. He also designed and implemented Generics for Java language. (Generics were in Java compiler since 1.3 but disabled until 1.5)http://www.scala-lang.org/index.html It was first released in 2003 and evolved a lot since then. It is has very active and growing community (combined Scala & Lift there are about 20K downloads per month) Scala code compiles to Java bytecode => Java class files therefore:  It runs under JVM (actually there is some outdated version for C# too)  It naturally interoperable with Java so one can implement/extend Java interfaces/classes, use any Java class in Scala code.  Compiled Scala code can be used from Java code just like any other Java compiled code … just drop the classes/jars in your classpath  Performance is pretty similar with Java’s. There is an Eclipse plugin (probably plugins for other IDE’s) on Scala site Documentation (Language specification, Scala By Example and many other) can be found on Scala site.

4 Scala language … Now let’s dig a bit in Scala language features Traits  A trait is basically like a Java interface. But… you can also write code in a trait, unlike a Java interface. trait Example { def doSomething(param: String): String def concatSomething(param: String): String = doSomething(param) + param }  So we defined two functions and implemented one  A class can extend this trait using extends keyword  Traits do not have constructors

5 Scala language … Compound types Having two traits trait Cloneable extends java.lang.Cloneable { override def clone(): Cloneable = { super.clone(); this } } trait Resetable { def reset: Unit = { // some freak-ish logic } } Now we can combine the two class CloneAndReset extends Cloneable with Resetable {... } A with B with C with … is said to be a compound type

6 Scala language … Pattern matching  Having abstract class Parent case class A(x:int, str: String) extends Parent case class B(x: long) extends Parent A case class is a class that can be used to pattern match it’s instances such as: val k: Parent = A(5, "Just me") val res = k match { case A(5, _) => 1 case B(1) => 2 case A(_, "Just me") => 3 } Of course there is a lot more about pattern matching but that goes beyond the scope of this presentation Type constructor polymorphism (a.k.a generics)  Of course Scala has support for generics like class A[T] {} where T is a type parameter a A a type constructor Abstract types class A { type T val member: T }

7 Scala language …Functional side In Scala everything is an object including literals and functions so Scala is a full OO language. Scala is a functional language as well in the sense that every function is a value therefore functional artifacts like higher order functions are naturally supported. Functional programming aspects require a different mindset than imperative programming (Java, C#, C++ etc.) object Main { def sum(numbers: List[Int]): Int = { var sum = 0 for (x <- numbers) { sum += x } sum } def main(args: Array[String]) { val numbers = 1 :: 3 :: 43 :: 23 :: Nil println(sum(numbers)) // Calculates the sum of numbers from a list. Imperative way println((0 /: numbers)(_ + _)) // Same result as above. Functional way // /: is a function equivalent with List#foldLeft which returns // f(... (f(f(z, a0), a1)...), an) if the list is [a0, a1,..., an]. println(numbers.foldLeft(0)(_ + _)) // same as above }

8 Scala language …Functional side Higher order functions def func(f: Int => String): String => Int = { Console.println(f(3)) val ret : String => Int = str => 20; ret } Come again? … Well this translates to: - func is a function that takes parameter another function that has an int parameter and returns a String - func returns a function that has a String parameter and return an int - ret is said to be a reference to an anonymous function - How do we call it? func(_ + " value")("myself) which returns an int. Pretty wicked ;) … but to understand it we need to de-sugar it for a bit. This is equivalent with func(x => x + " value")("myself"). But what is the type of x? … well it is inferred by the compiler and the above is equivalent with: func((x: Int) => x + " value")("myself") where we also specified the type of x.

9 Scala language …Functional side Currying – Form wikipedia “ In computer science, currying, invented by Moses Schönfinkel and Gottlob Frege, is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry). ”computer scienceMoses Schönfinkel Gottlob Fregefunctionarguments  So having f (a, b) f(a) returns a function that takes b’s type argument. (I couldn’t put it simpler than this …)  Taking the example from higher order functions above we already have a function returning a function. Scala likes this so much that it introduced a synctactic sugar for if so func can be written as: def func(f: int => String)(str: String):int = { Console.println(f(3)) 10 } We call it as above func(_ + " value")("myself") But now we didn’t explicitly returned a function but we returned an int func(_ + " value") returns another function that takes a String as argument and returns an int

10 Scala language …Functional side Closures  Basically a closure is a block of code that we can reference or pass as an argument to a function. (There are better definitions out there though)  Let’s look on this example: def loop(block: => Unit)(eval: => Boolean) { block if (eval){ loop(block)(eval) }  And using it like: var x = 0 loop { Console.println(x); x += 1 } (x < 10)

11 Scala language …Actors I first wanted to not include this … but I couldn’t help it Of course in Scala you can use Java Threads and so on but Scala includes an Actor library based on Erlang model. In short Actors are asynchronous components that interact with each other via messages. Actors scales way better then Threads (on a similar throughput we can get about 5000 Java threads but around 12000 actors with constant throughput).

12 Scala language …Actors Sending an asynchronous message: myActor ! Message(“Hello”) Receiving actor looks like: val myActor = actor { loop { react { case Message(text) => println(text) } case class Message(str: String)

13 LiftWeb is “yet another web framework” written in Scala. LiftWeb author is David Pollak and now there are about 15 committers and a community of over 1000 members LiftWeb site is http://liftweb.net/ and the public list http://groups.google.com/group/liftweb http://liftweb.net/http://groups.google.com/group/liftweb LiftWeb fundaments  Lift is a view-first framework  It sits on top of a servlet filter therefore it runs on any J2EE/Java EE web container  Lift is a Stateful framework  It is based on html/xhtml. NO JSP ! … that is wonderful as everything is so clean. No more cluttering due to Java code in JSP (JSP opened the Pandora’s box by allowing Java code directly in the markup)  It uses templates therefore page fragments can be developed independently and then included in certain places within templates  It has pluggable snippets (we’ll see a bit later what a snippet is)  Consistent Ajax support - In short Ajax sits on top of XMLHttpRequest JavaScript object. By this techniques a web application can behave very dynamic without the need for forms submissions and pages reload from server.  Integrated JQuery JavaScript library  Support for Comet (Comet is a concept by which server is pushing information to the browser. In web applications contexts Ajax is enabling Comet)  REST API support  ORM support – It has a module called Mapper by which we can define objects that map to a RDBMS representation. Also it is capable to create the DB schema for you based on the Mapper. The new version of the Mapper is called Record and it’s an evolutionary step. Of course one can use Scala’s DB module directly or plain JDBC directly.  No XML hell for configurations etc.  Lift is secure – resistant to XSS, session replay, parameter tampering

14 LiftWeb - project LiftWeb requires a certain structure. I.e.  src/main /scala  /bootstrap  /demo/helloworld/  /comet  /snippet  /view  /model /webapp  /templates-hidden  /WEB-INF  Html pages Creating a new Lift project with maven: mvn archetype:generate -U \ -DarchetypeGroupId=net.liftweb \ -DarchetypeArtifactId=lift-archetype-blank \ -DarchetypeVersion=1.0 \ -DremoteRepositories=http://scala- tools.org/repo-releases \ -DgroupId=demo.helloworld \ -DartifactId=helloworld \ -Dversion=1.0-SNAPSHOT

15 LiftWeb - Example Let’s discuss a simple Lift web-chat application.

16 Lift on Production From David Pollak 2K pages per second (basic pages with a couple of snippets) running on an Intel i7 920 system with Ubuntu 700 pages per second on an Amazon EC2 Large instance (dual core 2Ghz opteron) playing http://buyafeature.com/ (2,250 simultaneous participants) with a load average on the machine of 0.24 http://buyafeature.com/

17 Q&A Oh … and don’t forget http://apress.com/book/view/1430224215 http://apress.com/book/view/1430219890


Download ppt "An overview of Scala and Main Topics  Presentation intent.  What is Scala?  What is LiftWeb?  A trivial chat application  Q&A Author: Marius Danciu."

Similar presentations


Ads by Google