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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Lecture 11 Server Side Interaction
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
My First Building Block Presented By Tracy Engwirda 28 September, 2005.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fundamentals, Design, and Implementation, 9/e Chapter 14 JDBC, Java Server Pages, and MySQL.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
DT228/3 Web Development JSP: Directives and Scripting elements.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
UNIT-V The MVC architecture and Struts Framework.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
CPS120: Introduction to Computer Science The World Wide Web Nell Dale John Lewis.
The Scala Programming Language presented by Donna Malayeri.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
- Neeraj Chandra.  It’s language written by by Martin Odersky at EPFL  It’s language written by by Martin Odersky at EPFL (École Polytechnique Fédérale.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
JavaScript Introduction. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript A first JavaScript example Introduction to getElementById.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Plug-in Architectures Presented by Truc Nguyen. What’s a plug-in? “a type of program that tightly integrates with a larger application to add a special.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Modern Programming Language. Web Container & Web Applications Web applications are server side applications The most essential requirement.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Message Framework Topic subscribe for javascript/flex client.
OOP Basics Classes & Methods (c) IDMS/SQL News
Ur/Web: A Simple Model for Programming the Web
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
PySpark Tutorial - Learn to use Apache Spark with Python
Progress Apama Fundamentals
Applications Active Web Documents Active Web Documents.
The Object-Oriented Thought Process Chapter 13
The Share Widget Library
ITM352 PHP and Dynamic Web Pages: Server Side Processing 1.
Learning to Program D is for Digital.
Web Programming Developing Web Applications including Servlets, and Web Services using NetBeans 6.5 with GlassFish.V3.
Play Framework: Introduction
A very brief introduction
Spark Presentation.
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
AJAX.
CMPE419 Mobile Application Development
SharePoint-Hosted Apps and JavaScript
SVTRAININGS. SVTRAININGS Python Overview  Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed.
MVC Framework, in general.
Inheritance Basics Programming with Inheritance
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Chapter 9 Web Services: JAX-RPC, WSDL, XML Schema, and SOAP
What’s changed in the Shibboleth 1.2 Origin
Asynchronous Javascript And XML
ASP.NET Module Subtitle.
Objectives In this lesson you will learn about: Need for servlets
Computer Programming with JAVA
JavaServer Faces: The Fundamentals
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
…and web frameworks in general
JavaScript CS 4640 Programming Languages for Web Applications
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CMPE419 Mobile Application Development
C++ Object Oriented 1.
Plug-In Architecture Pattern
Presentation transcript:

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 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 …

What is Scala? Scala is a general purpose language, a hybrid OOP and Functional language invented by Martin Odersky from EPFL He also designed and implemented Generics for Java language. (Generics were in Java compiler since 1.3 but disabled until 1.5) 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.

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

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

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 }

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 }

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.

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

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)

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 actors with constant throughput).

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)

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 and the public list 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

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= tools.org/repo-releases \ -DgroupId=demo.helloworld \ -DartifactId=helloworld \ -Dversion=1.0-SNAPSHOT

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

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 (2,250 simultaneous participants) with a load average on the machine of

Q&A Oh … and don’t forget