7. The Da Vinci Machine Project
We are extending the JVM with first-class
architectural support for languages other than
Java, especially dynamic languages. This project
will prototype a number of extensions to the JVM, so
that it can run non-Java languages efficiently, with a
performance level comparable to that of Java itself.
8. The Da Vinci Machine Project
JSR 223: Scripting for the Java Platform
JSR 292: Supporting Dynamically Typed
Languages on the Java Platform
New JDK 7 instruction: invokedynamic
10. Ola Bini Programming Pyramid
/
/
/ DSL
/------
/
Dynamic
/
/------------
/
Stable
/
/------------------
11. Ola Bini Programming Pyramid
Domain-Specific (DSL, web templating)
Specific part of the application
Dynamic (Groovy, Clojure)
Rapid, productive,
flexible development or funcionality
Stable (Java, Scala)
Core funcionality, stable,
well-tested, performance
12. Civilization advances by extending the
number of important operations which we
can perform without thinking about them.
Alfred North Whitehead
19. All of them
â Everything is an object
â Operator overloading
â Native syntax for collection classes
â Regular expressions as first class citizens
â Closures
â Facilities to build DSLs
20. Groovy
â Dynamic Language
â Object-Oriented
â Designed for the JVM
â Inspired by Python, Ruby and Smalltalk
â Good integration with Java
â 2003
22. Scripting
Typically dynamic languages
No need to define variable before you use
them. Many type conversion
Most scripting languages are interpreted
Perform the script compilation and
execution within the same process
Fast results for small jobs
Write application faster
Execute commands repeatedly
23. Groovy Beans
class Person {
String name
Integer age
}
def person = new Person(name: 'John', age: 30)
def name = person.name
person.age = 35
24. Static vs. Dynamic
Static typing
A programming language is said to use static typing
when type checking is performed during compile time as
opposed to runtime
Dynamic typing
A programming language is said to be dynamically typed
when the majority of its type checking is performed at
runtime as opposed to at compile time
27. Java â Groovy
import java.util.List;
import java.util.ArrayList;
public class Filter {
public static void main(String[] args) {
List names = new ArrayList();
names.add("Ted"); names.add("Fred");
names.add("Jed"); names.add("Ned");
System.out.println(names);
Filter filter = new Filter();
List shortNames = filter.filterLongerThan(names, 3);
System.out.println(shortNames.size());
for (String item : shortNames) {
System.out.println(item);
}
}
...
28. Java â Groovy
...
private List filterLongerThan(List strings, int length) {
List result = new ArrayList();
for (String item : strings) {
if (item.length() <= length) {
result.add(item);
}
}
return result;
}
}
30. Static language
interface Animal {
def quack()
}
class Duck implements Animal {
def quack() {
println "I am a Duck"
}
}
class Frog implements Animal {
def quack() {
println "I am a Frog"
}
}
31. Polymorphism
Animal animal
animal = new Duck()
animal.quack()
===> I am a Duck
animal = new Frog()
animal.quack()
===> I am a Frog
32. Dynamic language
class Duck {
def quack() {
println "I am a Duck"
}
}
class Frog {
def quack() {
println "I am a Frog"
}
}
33. Duck typing
def animal
animal = new Duck()
animal.quack()
===> I am a Duck
animal = new Frog()
animal.quack()
===> I am a Frog
35. Dynamic Method Call
someObject."$methodName"()
def rex = new Dog()
doAction(rex, "bark")
===> woof!
doAction(rex, "jump")
===> boing!
36. Metaprogramming
Programs that write or manipulate
other programs
Compile time
â Groovy AST
Runtime
â Hook into method dispaching
â Dynamically create methods/properties
â Dynamic execution of expressions
37. AST Transformations
@Singleton
class Singleton {}
@EqualsAndHashCode
class Person {
String name, lastName
}
@Immutable
class Coordinates {
Double latitude, longitude
}
38. Method Missing
class Dog {
def bark() { println "woof!" }
def jump() { println "boing!" }
}
def rex = new Dog()
rex.sit()
ERROR
groovy.lang.MissingMethodException
40. Adding methods to a
class at runtime
4.toRoman()
ERROR
groovy.lang.MissingMethodException
41. Adding methods to a
class at runtime
SomeObject.metaClass.newMethod = { ->
// do something
}
Integer.metaClass.toRoman = { ->
println 'IV'
}
4.toRoman()
===> IV
42. Scala
â Object-Oriented and Functional Language
â Stronger type system
â Designed for the JVM
â Concurrency: Actors
â Many advanced language features
â As fast as Java
â 2003
47. Anatomy of a Function
def max (x: Int, y: Int): Int = {
if (x > y)
x
else
y
}
48. Type Inference
// Java
Map<String, List<Integer>> map =
new HashMap<String, List<Integer>>()
// Java 7
Map<String, List<Integer>> map =
new HashMap<>()
// Scala
val map = new HashMap[String, List[Int]]
49. Variable Declaration
var mutable = "I am mutable"
mutable = "Touch me, change me..."
val immutable = "I am not mutable"
immutable = "Can't touch this"
error: reassignment to val
50. Immutability
Simple
Immutable objects can only be in exactly
one state, the state in which it was created
Always consistent
Less prone to errors and more secure
Immutable objects can be shared freely
Freedom to cache
Inherently thread-safe
51. Imperative vs. Declarative
Imperative: how to achieve our goal
Take the next customer from a list.
If the customer lives in Spain, show their details.
If there are more customers in the list, go to the beginning
Declarative: what we want to achieve
Show customer details of every customer living in Spain
52. Mutability
val listOneToTen: List[Int] = {
var list: List[Int] = List()
var i: Int = 1
while (i <= 10) {
list :+= i
i += 1
}
list
}
53. Immutability
def listToTen(i: Int): List[Int] = {
if (i <= 10)
i :: listToTen(i + 1)
else
Nil
}
val listOneToTen = listToTen(1)
54. Range
scala> println(listOneToTen)
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(1 to 10) toList
55. Mixin Traits
class Person(val name: String)
trait Power {
def fly() = println("Fly")
}
class SuperHero(override val name:String)
extends Person(name) with Power
val superMan = new SuperHero("SuperMan")
scala> superMan.fly
Fly
56. Pattern Matching
def determineType(x:Any) = x match {
case s: String => println("String")
case l: List[_] => println("List")
case l: Map[_,_] => println("Map")
case _ => println("Unhandled type!")
}
def factorial(n: Int): Int = n match {
case 0 => 1
case x if x > 0 => factorial(n - 1) * n
}
57. Clojure
â Clojure is LISP
â Dynamic language
â Designed for the JVM
â Code is data
â Powerful macros
â Good interoperability with Java
â Clojure is fast
â Concurrency. Shared Transactional Memory
â 2007
58. Why Functional Programming?
Referential transparency
λ
Unit testing
λ
Debbuging
λ
Parallelization
λ
Modularity and composition
λ
Increases the quality of code
λ
Abstractions
λ
66. Lazy Evaluation
Only does as much work as necessary
Delays the evaluation of the expression
until it's needed
CPU efficient
The value is not calculated or assigned
until the value is requested
Manage potentially infinite data structures
Only a manageable subset of the data
will actually be used