際際滷

際際滷Share a Scribd company logo
Tecnologie Groovy 
Prima Parte 
Giugno 2013 
Francesco Lerro 
venerd狸 28 giugno 13
Obiettivi del talk 
 Mostrare le peculiarit del linguaggio 
 Introdurre alcuni tool legati a Groovy 
 Farvi venire voglia di provarlo :) 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = new ArrayList<String>(); 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs.get(i); 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results.add(s); 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = new ArrayList<String>(); 
! ! names.add("Giulio"); names.add("Laura"); 
! ! names.add("Mario"); names.add("Anna"); 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames.get(i); 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = new ArrayList<String>(); 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs.get(i); 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results.add(s); 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = new ArrayList<String>(); 
! ! names.add("Giulio"); names.add("Laura"); 
! ! names.add("Mario"); names.add("Anna"); 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames.get(i); 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = new ArrayList<String>(); 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs.get(i); 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results.add(s); 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = new ArrayList<String>(); 
! ! names.add("Giulio"); names.add("Laura"); 
! ! names.add("Mario"); names.add("Anna"); 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames.get(i); 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = new ArrayList<String>(); 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs.get(i); 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results.add(s); 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = new ArrayList<String>(); 
! ! names.add("Giulio"); names.add("Laura"); 
! ! names.add("Mario"); names.add("Anna"); 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames.get(i); 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = new ArrayList<String>(); 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs.get(i); 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results.add(s); 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = new ArrayList<String>(); 
! ! names.add("Giulio"); names.add("Laura"); 
! ! names.add("Mario"); names.add("Anna"); 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames.get(i); 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
! static List<String> filterLongerThan(List<String> xs, int maxLen) { 
! ! List<String> results = [] 
! ! for (int i=0; i < xs.size(); i++){ 
! ! ! String s = xs[i] 
! ! ! if (s.length() <= maxLen){ 
! ! ! ! results << s 
! ! ! } 
! ! } 
! ! return results; 
! } 
! public static void main(String[] args) { 
! ! List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
! ! System.out.println(names); 
! ! List<String> shortNames = filterLongerThan(names, 5); 
! ! System.out.println("After filtering: " + shortNames.size()); 
! ! for (int i=0; i < shortNames.size(); i++) { 
! ! ! String s = shortNames[i] 
! ! ! System.out.println(s); 
! ! } 
! } 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
List<String> results = [] 
for (int i=0; i < xs.size(); i++){ 
String s = xs[i] 
if (s.length() <= maxLen){ 
results << s 
} 
} 
return results; 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
System.out.println(names); 
List<String> shortNames = filterLongerThan(names, 5); 
System.out.println("After filtering: " + shortNames.size()); 
for (int i=0; i < shortNames.size(); i++) { 
String s = shortNames[i]; 
System.out.println(s); 
} 
} 
} 
venerd狸 28 giugno 13
import java.util.List; 
import java.util.ArrayList; 
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
List<String> results = [] 
for (int i=0; i < xs.size(); i++){ 
String s = xs[i] 
if (s.length() <= maxLen){ 
results << s 
} 
} 
return results; 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
System.out.println(names); 
List<String> shortNames = filterLongerThan(names, 5); 
System.out.println("After filtering: " + shortNames.size()); 
for (int i=0; i < shortNames.size(); i++) { 
String s = shortNames[i]; 
System.out.println(s); 
} 
} 
} 
venerd狸 28 giugno 13
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
List<String> results = [] 
xs.each { s -> 
if (s.length() <= maxLen){ 
results << s 
} 
} 
return results; 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
println(names); 
List<String> shortNames = filterLongerThan(names, 5); 
println("After filtering: " + shortNames.size()); 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
List<String> results = [] 
results = xs.findAll { s -> s.length() <= maxLen } 
return results; 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
println(names) 
List<String> shortNames = filterLongerThan(names, 5) 
println("After filtering: " + shortNames.size()) 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
xs.findAll { s -> s.length() <= maxLen } 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
println(names); 
List<String> shortNames = filterLongerThan(names, 5); 
println("After filtering: " + shortNames.size()); 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
xs.findAll { s -> s.length() <= maxLen } 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
println(names) 
List<String> shortNames = filterLongerThan(names, 5) 
println("After filtering: " + shortNames.size()) 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
class Filter { 
static List<String> filterLongerThan(List<String> xs, int maxLen) { 
xs.findAll { s -> s.length() <= maxLen } 
} 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
println(names) 
List<String> shortNames = filterLongerThan(names, 5) 
println("After filtering: ${shortNames.size()}") 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
class Filter { 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
int maxLen = 5 
println(names) 
List<String> shortNames = names.findAll { s -> s.length() <= maxLen } 
println("After filtering: ${shortNames.size()}") 
shortNames.each { println(it) } 
} 
} 
-70% 
venerd狸 28 giugno 13
class Filter { 
public static void main(String[] args) { 
List<String> names = ["Giulio", "Laura", "Mario", "Anna"] 
int maxLen = 5 
println(names) 
List<String> shortNames = names.findAll { s -> s.length() <= maxLen } 
println("After filtering: ${shortNames.size()}") 
shortNames.each { println(it) } 
} 
} 
venerd狸 28 giugno 13
+ 
Smalltalk { 
venerd狸 28 giugno 13
+ 
optionally typed, 
dinamico, 
funzionale 
venerd狸 28 giugno 13
Opzionalmente tipizzato 
venerd狸 28 giugno 13
Dinamico 
 Aggiunta a compile-time di funzionalit: 
metodi, propiet, costruttori 
 Runtime mixins ed invocazione di metodi il 
cui nome risiede in una variabile 
venerd狸 28 giugno 13
Dinamico - ExpandoMetaClass 
venerd狸 28 giugno 13
Dinamico - ExpandoMetaClass 
Aggiunta funzionalit a 
compile-time anche se final 
venerd狸 28 giugno 13
Dinamico - Metodi su collection 
venerd狸 28 giugno 13
Dinamico - Mixin di metodi 
@Category(Vehicle) class FlyingAbility { 
def fly() { "${name}: fly!" } 
} 
@Category(Vehicle) class DivingAbility { 
def dive() { "${name}: dive!" } 
} 
interface Vehicle { 
String getName() 
} 
@Mixin(FlyingAbility) 
class Plane implements Vehicle { 
String getName() { "Concorde" } 
} 
@Mixin([DivingAbility, FlyingAbility]) 
class JamesBondVehicle implements Vehicle { 
String getName() { "James Bond's vehicle" } 
} 
assert new Plane().fly() == "Concorde: fly!" 
assert new JamesBondVehicle().fly() == "James Bond's vehicle: fly!" 
assert new JamesBondVehicle().dive() == "James Bond's vehicle: dive!" 
venerd狸 28 giugno 13
Dinamico - Via annotation 
 Iniezione funzionalit via annotation 
 @Immutable (@Canonical) 
 @Bindable 
 @Delegate 
 @Newify 
 @Lazy 
 @Log 
 etc. 
venerd狸 28 giugno 13
Dinamico - Esempio @Delegate 
venerd狸 28 giugno 13
Funzionale 
 Funzioni come first class citizens (Closure) 
 associabili ad una variabile 
 usate come parametri di altre funzioni 
 componibili 
 zucchero sintattico 
venerd狸 28 giugno 13
Funzionale - Esempio di currying 
venerd狸 28 giugno 13
La Groovy verit 
 Regole per la coercizione in boolean 
 Collezioni: true se NON vuote 
 Stringhe: true se NON vuote o nulle 
 Oggetti: true se NON nulli 
 Regexp Pattern: true se MATCH 
venerd狸 28 giugno 13
Groovy truth - Esempi 
venerd狸 28 giugno 13
Groovy truth - Esempi 
Usa gli operatori ?. e ?: in 
combinazione per evitare NPE 
venerd狸 28 giugno 13
Builder 
venerd狸 28 giugno 13
Builder 
venerd狸 28 giugno 13
Builder 
venerd狸 28 giugno 13
Slurper 
<?xml version="1.0" ?> 
<customers> 
<customer name="Mario Rossi" /> 
<customer name="Giovanni Bianchi" /> 
</customers> 
import java.io.*; 
import javax.xml.parsers.*; 
import org.w3c.dom.*; 
public class XMLReader { 
public static void main(String argv[]) throws Exception { 
File file = new File("customers.xml"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(file); 
doc.getDocumentElement().normalize(); 
NodeList nodeLst = doc.getElementsByTagName("customer"); 
for (int s = 0; s < nodeLst.getLength(); s++) { 
Element item = (Element) nodeLst.item(s); 
System.out.println(item.getAttribute("name")); 
} 
} 
} 
venerd狸 28 giugno 13
Slurper 
<?xml version="1.0" ?> 
<customers> 
<customer name="Mario Rossi" /> 
<customer name="Giovanni Bianchi" /> 
</customers> 
def xmlFile = new File('customers.xml') 
def xml = new XmlSlurper().parse(xmlFile) 
xml.customer.each { 
println it.'@name'.text() 
} 
Anche per JSON, Config, ... 
venerd狸 28 giugno 13
Performance 
 Dinamico implica overhead 
 spesso diversi punti % 
 Molti miglioramenti da Groovy 2.0 
 primitiva invokeDynamic (Java 7) 
 @CompileStatic 
 Valutare trade-off produttivit VS velocit 
venerd狸 28 giugno 13
Performance... 
@Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1') 
import gbench.BenchmarkBuilder 
import groovy.transform.CompileStatic 
int fib(int n) { 
if (n < 2) return n 
return fib(n - 1) + fib(n - 2) 
} 
@CompileStatic 
int fib2(int n) { 
if (n < 2) return n 
return fib2(n - 1) + fib2(n - 2) 
} 
new BenchmarkBuilder().run { 
int n = 20 
"Normal Version" { fib n } 
"@CompileStatic Version" { fib2 n } 
}.prettyPrint() 
venerd狸 28 giugno 13
Performance... 
@Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1') 
import gbench.BenchmarkBuilder 
import groovy.transform.CompileStatic 
int fib(int n) { 
if (n < 2) return n 
return fib(n - 1) + fib(n - 2) 
} 
@CompileStatic 
int fib2(int n) { 
if (n < 2) return n 
return fib2(n - 1) + fib2(n - 2) 
} 
new BenchmarkBuilder().run { 
int n = 20 
"Normal Version" { fib n } 
"@CompileStatic Version" { fib2 n } 
}.prettyPrint() 
* JVM: Java HotSpot(TM) 64-Bit Server VM (23.6-b04... 
* JRE: 1.7.0_10 
* Total Memory: 81.1875 MB 
* Maximum Memory: 1123.5625 MB 
* OS: Mac OS X (10.7.5, x86_64) 
Options 
======= 
* Warm Up: Auto 
* CPU Time Measurement: On 
user system cpu real 
Normal Version 92622 7 92630 92626 
@CompileStatic Version 39974 3 39977 39976 
venerd狸 28 giugno 13
Ecosistema 
Geb, Spock, WSLite... 
venerd狸 28 giugno 13
Ecosistema 
Nella seconda parte 
approfondiremo il discorso 
su queste tecnologie! 
Geb, Spock, WSLite... 
venerd狸 28 giugno 13
Riferimenti 
 Groovy User Guide - http://groovy.codehaus.org/User+Guide 
 Bob Brown - The future is Gr8 - http://wordpress.transentia.com.au/wordpress/ 
2013/05/07/disaster/ 
 Sergey Dolgopolov - Testing the performance of new Groovy 2.0 release with 
GBench - http://www.sergeydolgopolov.me/2012/07/groovy-20-has-been-released-testing- 
new.html 
 Guillame Laforge - Groovy Ecosystem - http://www.slideshare.net/glaforge/groovy-ecosystem- 
jfokus-2011-guillaume-laforge 
 Hubert Klein Ikkink (aka mrhaki) - Groovy goodness blog - 
http://mrhaki.blogspot.it/ 
 Geb Samples - http://www.gebish.org 
 GPars Samples for Dataflow and Actors - http://gpars.codehaus.org 
 Spock Samples - http://code.google.com/p/spock/ 
 Tim Myer - http://timezra.blogspot.it/2011/11/trampoline-and-memoize.html 
venerd狸 28 giugno 13
Web: http://rolandfg.net Twitter: @flerro 
venerd狸 28 giugno 13

More Related Content

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...
Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray

Groovy: il linguaggio

  • 1. Tecnologie Groovy Prima Parte Giugno 2013 Francesco Lerro venerd狸 28 giugno 13
  • 2. Obiettivi del talk Mostrare le peculiarit del linguaggio Introdurre alcuni tool legati a Groovy Farvi venire voglia di provarlo :) venerd狸 28 giugno 13
  • 3. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = new ArrayList<String>(); ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs.get(i); ! ! ! if (s.length() <= maxLen){ ! ! ! ! results.add(s); ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = new ArrayList<String>(); ! ! names.add("Giulio"); names.add("Laura"); ! ! names.add("Mario"); names.add("Anna"); ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames.get(i); ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 4. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = new ArrayList<String>(); ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs.get(i); ! ! ! if (s.length() <= maxLen){ ! ! ! ! results.add(s); ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = new ArrayList<String>(); ! ! names.add("Giulio"); names.add("Laura"); ! ! names.add("Mario"); names.add("Anna"); ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames.get(i); ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 5. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = new ArrayList<String>(); ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs.get(i); ! ! ! if (s.length() <= maxLen){ ! ! ! ! results.add(s); ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = new ArrayList<String>(); ! ! names.add("Giulio"); names.add("Laura"); ! ! names.add("Mario"); names.add("Anna"); ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames.get(i); ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 6. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = new ArrayList<String>(); ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs.get(i); ! ! ! if (s.length() <= maxLen){ ! ! ! ! results.add(s); ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = new ArrayList<String>(); ! ! names.add("Giulio"); names.add("Laura"); ! ! names.add("Mario"); names.add("Anna"); ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames.get(i); ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 7. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = new ArrayList<String>(); ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs.get(i); ! ! ! if (s.length() <= maxLen){ ! ! ! ! results.add(s); ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = new ArrayList<String>(); ! ! names.add("Giulio"); names.add("Laura"); ! ! names.add("Mario"); names.add("Anna"); ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames.get(i); ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 8. import java.util.List; import java.util.ArrayList; class Filter { ! static List<String> filterLongerThan(List<String> xs, int maxLen) { ! ! List<String> results = [] ! ! for (int i=0; i < xs.size(); i++){ ! ! ! String s = xs[i] ! ! ! if (s.length() <= maxLen){ ! ! ! ! results << s ! ! ! } ! ! } ! ! return results; ! } ! public static void main(String[] args) { ! ! List<String> names = ["Giulio", "Laura", "Mario", "Anna"] ! ! System.out.println(names); ! ! List<String> shortNames = filterLongerThan(names, 5); ! ! System.out.println("After filtering: " + shortNames.size()); ! ! for (int i=0; i < shortNames.size(); i++) { ! ! ! String s = shortNames[i] ! ! ! System.out.println(s); ! ! } ! } } venerd狸 28 giugno 13
  • 9. import java.util.List; import java.util.ArrayList; class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { List<String> results = [] for (int i=0; i < xs.size(); i++){ String s = xs[i] if (s.length() <= maxLen){ results << s } } return results; } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] System.out.println(names); List<String> shortNames = filterLongerThan(names, 5); System.out.println("After filtering: " + shortNames.size()); for (int i=0; i < shortNames.size(); i++) { String s = shortNames[i]; System.out.println(s); } } } venerd狸 28 giugno 13
  • 10. import java.util.List; import java.util.ArrayList; class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { List<String> results = [] for (int i=0; i < xs.size(); i++){ String s = xs[i] if (s.length() <= maxLen){ results << s } } return results; } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] System.out.println(names); List<String> shortNames = filterLongerThan(names, 5); System.out.println("After filtering: " + shortNames.size()); for (int i=0; i < shortNames.size(); i++) { String s = shortNames[i]; System.out.println(s); } } } venerd狸 28 giugno 13
  • 11. class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { List<String> results = [] xs.each { s -> if (s.length() <= maxLen){ results << s } } return results; } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] println(names); List<String> shortNames = filterLongerThan(names, 5); println("After filtering: " + shortNames.size()); shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 12. class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { List<String> results = [] results = xs.findAll { s -> s.length() <= maxLen } return results; } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] println(names) List<String> shortNames = filterLongerThan(names, 5) println("After filtering: " + shortNames.size()) shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 13. class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { xs.findAll { s -> s.length() <= maxLen } } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] println(names); List<String> shortNames = filterLongerThan(names, 5); println("After filtering: " + shortNames.size()); shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 14. class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { xs.findAll { s -> s.length() <= maxLen } } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] println(names) List<String> shortNames = filterLongerThan(names, 5) println("After filtering: " + shortNames.size()) shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 15. class Filter { static List<String> filterLongerThan(List<String> xs, int maxLen) { xs.findAll { s -> s.length() <= maxLen } } public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] println(names) List<String> shortNames = filterLongerThan(names, 5) println("After filtering: ${shortNames.size()}") shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 16. class Filter { public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] int maxLen = 5 println(names) List<String> shortNames = names.findAll { s -> s.length() <= maxLen } println("After filtering: ${shortNames.size()}") shortNames.each { println(it) } } } -70% venerd狸 28 giugno 13
  • 17. class Filter { public static void main(String[] args) { List<String> names = ["Giulio", "Laura", "Mario", "Anna"] int maxLen = 5 println(names) List<String> shortNames = names.findAll { s -> s.length() <= maxLen } println("After filtering: ${shortNames.size()}") shortNames.each { println(it) } } } venerd狸 28 giugno 13
  • 18. + Smalltalk { venerd狸 28 giugno 13
  • 19. + optionally typed, dinamico, funzionale venerd狸 28 giugno 13
  • 21. Dinamico Aggiunta a compile-time di funzionalit: metodi, propiet, costruttori Runtime mixins ed invocazione di metodi il cui nome risiede in una variabile venerd狸 28 giugno 13
  • 22. Dinamico - ExpandoMetaClass venerd狸 28 giugno 13
  • 23. Dinamico - ExpandoMetaClass Aggiunta funzionalit a compile-time anche se final venerd狸 28 giugno 13
  • 24. Dinamico - Metodi su collection venerd狸 28 giugno 13
  • 25. Dinamico - Mixin di metodi @Category(Vehicle) class FlyingAbility { def fly() { "${name}: fly!" } } @Category(Vehicle) class DivingAbility { def dive() { "${name}: dive!" } } interface Vehicle { String getName() } @Mixin(FlyingAbility) class Plane implements Vehicle { String getName() { "Concorde" } } @Mixin([DivingAbility, FlyingAbility]) class JamesBondVehicle implements Vehicle { String getName() { "James Bond's vehicle" } } assert new Plane().fly() == "Concorde: fly!" assert new JamesBondVehicle().fly() == "James Bond's vehicle: fly!" assert new JamesBondVehicle().dive() == "James Bond's vehicle: dive!" venerd狸 28 giugno 13
  • 26. Dinamico - Via annotation Iniezione funzionalit via annotation @Immutable (@Canonical) @Bindable @Delegate @Newify @Lazy @Log etc. venerd狸 28 giugno 13
  • 27. Dinamico - Esempio @Delegate venerd狸 28 giugno 13
  • 28. Funzionale Funzioni come first class citizens (Closure) associabili ad una variabile usate come parametri di altre funzioni componibili zucchero sintattico venerd狸 28 giugno 13
  • 29. Funzionale - Esempio di currying venerd狸 28 giugno 13
  • 30. La Groovy verit Regole per la coercizione in boolean Collezioni: true se NON vuote Stringhe: true se NON vuote o nulle Oggetti: true se NON nulli Regexp Pattern: true se MATCH venerd狸 28 giugno 13
  • 31. Groovy truth - Esempi venerd狸 28 giugno 13
  • 32. Groovy truth - Esempi Usa gli operatori ?. e ?: in combinazione per evitare NPE venerd狸 28 giugno 13
  • 33. Builder venerd狸 28 giugno 13
  • 34. Builder venerd狸 28 giugno 13
  • 35. Builder venerd狸 28 giugno 13
  • 36. Slurper <?xml version="1.0" ?> <customers> <customer name="Mario Rossi" /> <customer name="Giovanni Bianchi" /> </customers> import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class XMLReader { public static void main(String argv[]) throws Exception { File file = new File("customers.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); NodeList nodeLst = doc.getElementsByTagName("customer"); for (int s = 0; s < nodeLst.getLength(); s++) { Element item = (Element) nodeLst.item(s); System.out.println(item.getAttribute("name")); } } } venerd狸 28 giugno 13
  • 37. Slurper <?xml version="1.0" ?> <customers> <customer name="Mario Rossi" /> <customer name="Giovanni Bianchi" /> </customers> def xmlFile = new File('customers.xml') def xml = new XmlSlurper().parse(xmlFile) xml.customer.each { println it.'@name'.text() } Anche per JSON, Config, ... venerd狸 28 giugno 13
  • 38. Performance Dinamico implica overhead spesso diversi punti % Molti miglioramenti da Groovy 2.0 primitiva invokeDynamic (Java 7) @CompileStatic Valutare trade-off produttivit VS velocit venerd狸 28 giugno 13
  • 39. Performance... @Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1') import gbench.BenchmarkBuilder import groovy.transform.CompileStatic int fib(int n) { if (n < 2) return n return fib(n - 1) + fib(n - 2) } @CompileStatic int fib2(int n) { if (n < 2) return n return fib2(n - 1) + fib2(n - 2) } new BenchmarkBuilder().run { int n = 20 "Normal Version" { fib n } "@CompileStatic Version" { fib2 n } }.prettyPrint() venerd狸 28 giugno 13
  • 40. Performance... @Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1') import gbench.BenchmarkBuilder import groovy.transform.CompileStatic int fib(int n) { if (n < 2) return n return fib(n - 1) + fib(n - 2) } @CompileStatic int fib2(int n) { if (n < 2) return n return fib2(n - 1) + fib2(n - 2) } new BenchmarkBuilder().run { int n = 20 "Normal Version" { fib n } "@CompileStatic Version" { fib2 n } }.prettyPrint() * JVM: Java HotSpot(TM) 64-Bit Server VM (23.6-b04... * JRE: 1.7.0_10 * Total Memory: 81.1875 MB * Maximum Memory: 1123.5625 MB * OS: Mac OS X (10.7.5, x86_64) Options ======= * Warm Up: Auto * CPU Time Measurement: On user system cpu real Normal Version 92622 7 92630 92626 @CompileStatic Version 39974 3 39977 39976 venerd狸 28 giugno 13
  • 41. Ecosistema Geb, Spock, WSLite... venerd狸 28 giugno 13
  • 42. Ecosistema Nella seconda parte approfondiremo il discorso su queste tecnologie! Geb, Spock, WSLite... venerd狸 28 giugno 13
  • 43. Riferimenti Groovy User Guide - http://groovy.codehaus.org/User+Guide Bob Brown - The future is Gr8 - http://wordpress.transentia.com.au/wordpress/ 2013/05/07/disaster/ Sergey Dolgopolov - Testing the performance of new Groovy 2.0 release with GBench - http://www.sergeydolgopolov.me/2012/07/groovy-20-has-been-released-testing- new.html Guillame Laforge - Groovy Ecosystem - http://www.slideshare.net/glaforge/groovy-ecosystem- jfokus-2011-guillaume-laforge Hubert Klein Ikkink (aka mrhaki) - Groovy goodness blog - http://mrhaki.blogspot.it/ Geb Samples - http://www.gebish.org GPars Samples for Dataflow and Actors - http://gpars.codehaus.org Spock Samples - http://code.google.com/p/spock/ Tim Myer - http://timezra.blogspot.it/2011/11/trampoline-and-memoize.html venerd狸 28 giugno 13
  • 44. Web: http://rolandfg.net Twitter: @flerro venerd狸 28 giugno 13