Vad ar Groovy och Grails

  • View
    968

  • Download
    2

Embed Size (px)

DESCRIPTION

This presentation (in Swedish) gives a gentle introduction to the programming langauge Groovy and the web framework Grails. It starts by setting the language into its context of language evolution in general and JVM languages in particular.

Transcript

  • 1. i samarbete med
    Vad r Groovy och Grails?
    Jens Riboe
    September 2009

2. Vem r Jens Riboe?
Frliteolikakunder
3. Varfr?
4. Produktivitetsfrbttring
Development Speed
Konstruktionstidenfr en webbapplikation med feature X
Fem r sedan: 1-2 mnader
Idag: 1-2 dagar
5. Sprkutvecklingen
MLSchemeHaskel
Functional
programming
Pragmatic
FP & OOP
LISP
STL & JGL
-kalkyl
Script
Languages
Perl
ModulaPascalAda
Python
PHP
Perl5
Module
Dynamic
Languages
SmallTalk
Object
Oriented
Programming
JavaScript
Class
Simula
RubyGroovyJRubyScalaClojurePerl6
Objective-C
Abstract
data type
Java
Subroutine
C
C++
Fortran
Cobol
1960
1970
1980
1990
2000
2010
6. JVM blir en plattform fr mer n Java
Nyasprkovanpellerbredvid Java
AnvnderJVM:ensomexekveringsmaskin
Psikttonar Java bortssom C gjordenr C++ kom
7. JVM Sprk
8. Scala
Martin Odersky
cole Polytechnique Fdrale de Lausanne (EPFL)
2004
Multi-paradigm
Object-Oriented programming
Java + Mixin och Traits
Functional programming
Closures och Pattern Matching
Static typing
Kompilatorn hittar mnga fel
Extendible
Ltt att skapa DSL
9. JRubyochJython
Mngascriptsprkfinnsporterade till JVM:en
JRubyr en porteringav Ruby
Jythonr en porteringav Python
Ruby on Rails webbramverketvar trigger
Skapadestortintressefr
Rails somramverk
Ruby somsprk
10. ClojureochJaskel
RenodladefunctionellasprkpJVM:en
Clojurer en LISP variant
Jaskelr en porteringavHaskel
11. Groovy
12. Groovy
James Strachan
2003
http://radio.weblogs.com/0112098/2003/08/29.html
Inspireradav Ruby och Python
Den 'naturliga' vidareutvecklingenav Java
Superset av Java
Ssom C vergicki C++
Egenskaper
Dynamiskt (duck typing)
Objektorienterat (classes)
Funktionellt (closures)
13. Bttren Java
Betydligtmindre text attskriva
Duck typing
Trevliganyaoperatorer
Closures
Groovy beans
Groovy string
Regex
Categories
Operator overloading
Builders
Nyalibsoch tools kommerhelatiden
14. Ducktyping
If it walks like a duck and it quacks like a duck, then it probably is as a duck
Metodanrop p objekt utan krav p att tillhra viss typ
def min(a, b) { a t1.deadline t2.deadline}
18. Spread operator *.
I stllet fr
List txt = new ArrayList();
for (Todo t : todos) {
txt.add(t.getText());
}
System.out.println("txt = " + txt);
rcker det med
deftxt = todos*.text
printlntxt
19. List ochMap och Range
Del avsprket
List (java.util.*List)
Map (java.util.*Map)
deflst = [17, 'foo', 'bar', 'fee']
assertlst[0] == 17
assertlst[-2] == 'bar'
assertlst[1..2] == ['foo', 'bar']
def tbl = [one:1, two:2, three:3, four:4]
assert tbl.one == 1
assert tbl.four == 4
20. Closures
Anonymafunktionsblock
def nums = (1..10)
printlnnums
printlnnums.collect {it * it}
printlnnums.findAll {n -> n % 2}
printlnnums.inject(0) {sum, n -> sum + n}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 3, 5, 7, 9]
55
21. GroovyBeans
I stllet fr
import java.util.Date;
public classTodo {
private Stringtext;
private Datedeadline;
public Todo(String t, Date d) {text=t; deadline=d;}
public String getText() {return text;}
public voidsetText(String t) {text=t;}
public Date getDeadline() {return deadline;}
public voidsetDeadline(Date d) {deadline=d;}
};
rcker det med
classTodo {
Stringtext
Datedeadline
}
22. Anvndning av Groovy Beans
def t = new Todo(deadline: new Date() + 3,
text: 'Fix that bug')
t.text = 'Write that Groovy script'
deftodos = [new Todo(text:'Skip Java'),
new Todo(text:'Hack w Groovy'),
new Todo(text:'Web w Grails')]
todos = todos.sort {t1,t2 -> t1.deadline t2.deadline}
printlntodos*.text.join(". ")
Skip Java.
Hack w Groovy.
Web w Grails
23. Categories
Ett stt att temporrt addera metoder till en klass
def today = new Date()
println "Today: ${today}"
println "Today: ${today.iso8601()}"
Today: Sat Sep 05 14:19:13 CEST 2009
MissingMethodException: No signature of method: java.util.Date.iso8601()
import java.text.SimpleDateFormat
classFmtBoost {
static String iso8601(Date self) {
new SimpleDateFormat('yyyy-MM-dd').format(self)
}
static String getTraditional(Date self) {
new SimpleDateFormat('d MMM yyyy').format(self)
}
}
def today = new Date()
use (FmtBoost) {
println "Today: ${today.iso8601()}"
println "Today: ${today.traditional}"
}
Today: 2009-09-05
Today: 5 sep 2009
24. Anvndning av FmtBoost
classDoit {
Stringtext
Datedeadline = new Date()
}
deflst = [
new Doit(text: 'Read that groovybook'),
new Doit(text: 'Start usingbuilders'),
new Doit(text: 'Investigategroovyxml')
]
intday = 1
lst.each {it.deadline += day++}
use (FmtBoost) {
lst.each {println "${it.deadline.traditional}: ${it.text}"}
}
6 sep 2009: Read that groovy book
7 sep 2009: Start using builders
8 sep 2009: Investigate groovy xml
25. Builders
Kombinationav closures och meta-programmering
Inkrementellkonstruktionav composite structures
Exempel
XML och HTML
Swing
Ant
26. Generera XML
use(FmtBoost) {
defbuf = new StringWriter()
defxml = new MarkupBuilder(buf)
xml.'doit-list' {
lst.each {
doit(deadline: it.deadline.iso8601(), it.text)
}
}
printlnbuf
}

Read that groovy book
Start using builders
Investigate groovy xml

27. Processa XML
def news(url, phrase) {
def items = new XmlParser().parse(url).channel.item
if (phrase) {
items = items.grep { it.title.text().contains(phrase) }
}
items.collect {n -> [
title: n.title.text() - " ",
date: n.pubDate.text(), url: n.link.text(),
text: n.description.text()
]}
}



Yahoo! News

Foxaddson-airtweets to ...
http://...
Fri, 04 Sep 2009 02:54:51 GMT
Summerreruns are ...

...
...


28. Result av news(url, phrase)
def lst = news('http://rss.news.yahoo.com/rss/tech', 'Google')
lst.each {
it.text = it.text.replaceAll('
', '')
it.text = it.text.replaceAll(/(

)|(

)/, '')
it.text = it.text.replaceAll(/