SlideShare a Scribd company logo
Learning Groovy (in 3 hours)!
Adam L. Davis
The Solution Design Group, Inc.
Author of “Learning Groovy”
(& What’s New in Java 8 & others)
14 years Java Dev.
github.com/adamldavis
/2017-gr8conf-learning-groovy
Java ~ Brian Goetz (Java Language Architect)
Groovy
<Insert your
hated language
here>
PHP
groovy-lang.org
sdkman.io
● Dynamic or Static
● (@CompileStatic @TypeChecked)
● As fast as Java (with static & indy)
● Meta-programming
● Optional semi-colons
● Optional parentheses
● Short-hand for Lists and Maps
● Automatic getters and setters
● A better switch
● Groovy GDK…
Groovy 2.4 Features
● Closures
● Currying
● Method references
● Map/Filter/Reduce as collect, findAll, inject
● Internal iterating using each
● Operator Overloading (+ - * % / …)
● methodMissing and propertyMissing
● AST Transformations
● Traits
…
Groovy 2.4 Features (cont.)
Starting Out
Option 1: Using sdkman.io
– sdk install groovy 2.4.9
Option 2: Download from groovy-lang.org
– Alter your PATH
● Export PATH=$PATH:/usr/lib/groovy/bin
● Option 3: Mac – see http://groovy-lang.org/install.html
● Option π: Windows – see https://github.com/groovy/groovy-windows-installer
Then: $ groovyConsole
IntelliJ IDEA or NetBeans
Dynamic typing
● def keyword
● Parameters’ typing optional
● Possible to mock using a map
– def dog = [bark: { println ‘woof’ }]
● Using @TypeChecked or @CompileStatic you
can make Groovy statically typed in some
classes
Groovy Strings
● ‘normal string’
● “groovy string can contain $variables”
● “can also do expressions ${x + 1}”
● Use triple quote to start/end multi-line strings
‘’’
This is a
Multi-line
String
‘’’
Math, Groovy Truth, and Equals
● Numbers are BigDecimal by default not Double
– 3.14 is a BigDecimal
– 3.14d is a Double
● Groovy truth: null, “”, [], 0 are false
– if (!thing) println “thing was null”
– if (!str) println “str was empty”
● Groovy == means .equals
– For identity use a.is(b)
Property Access
● Everything is public
by default
● Every field has a
getter and setter by
default
● Gotcha’s
– Map access
– String.class->String
● Property access automatically
uses getters and setters
● foo.bar == foo.getBar()
● foo.bar = 2 == foo.setBar(2)
Lists and Maps
● def emptyList = [ ]
● def emptyMap = [:]
● def numList = [1,2,3]
● def strMap = [cars: 1, boats: 2, planes: 3]
● def varMap = [(var1): 1, (var2): 2, (var3): 3]
Maps Continued...
● def map = [cars: 1, boats: 2, planes: 3]
● String key access: map.cars
● OR map[‘cars’]
● Also works for modifying:
– map.cars = 42
– map[‘cars’] = 42
Code Demo
A better switch
● Switch can use types, lists, ranges, patterns…
Switch (x) {
case Map: println “was a map”; break
case [4,5,6]: println “was 4, 5 or 6”; break
case 0..20: println “was 0 to 20”; break
case ~/w+/: println “ was a word”; break
case “hello”: println x; break
case BigDecimal: println “was a BigDecimal”
Groovy GDK
● Adds methods to everything! Adds its own classes...
● Collections: sort, findAll, collect, inject, each,…
● IO: toFile(), text, bytes, withReader, URL.content
● Ranges: x..y, x..<y
– GetAt syntax for Strings and Lists:
● text[0..4] == text.substring(0,5)
● Utilities: ConfigSlurper, Expando, ObservableList/Map/Set
Safe dereference & Elvis operator
● Safe dereference ?.
– String name = person?.name
– Java: person == null ? null : person.getName()
● Elvis operator ?:
– String name = person?.name ?: “Bob”
– Java: if (name == null) name = “Bob”
Closures
● Closure: “a self-containing method” (like Lambda exp.)
– def say = { x -> println x }
– say(‘hello gr8conf’)
– def say = { println it }
– def adder = { x, y -> x + y }
● Closures have several implicit variables:
– it - If the closure has one argument
– this - Refers to the enclosing class
– owner - The same as this unless it is enclosed in another closure.
– delegate - Usually the same as owner but you can change it (this
allows the methods of delegate to be in scope).
Closures Continued...
● When used as last parameter, closure can go
outside parentheses
– methodCalled(param1, param2) { closureHere() }
– methodWithOnlyClosure { closureHere() }
Regex Pattern Matching
● Regex = regular expressions
● Within forward slashes / is a regex
– You don’t need to use double 
● =~ for matching anywhere within a string
– if (text =~ /d+/) println “there was a number in it”
● ==~ for matching the whole string
– if (email ==~ /[w.]+@[w.]+/) println “it’s an email”
Meta-programming
● Every class and instance has a metaClass
● String.metaClass.upper =
{ delegate.toUpperCase() }
– “foo”.upper() == “FOO”
● Traits can be used as
mixins
● Maps can be cast to
actual types using as
[bark: {println “Woof!”}]
as Dog
Code Demo
Other Surprises for Java Devs
● Default values for method parameters
● groovy.transform.*
– @EqualsAndHashCode
– @TupleConstructor
– @ToString
– @Canonical
● Generics not enforced by default
Advanced Groovy
● Groovy Design Patterns
– Strategy pattern
– Categories
– Method caching
● DSLs – Domain Specific Languages
– Operator Overloading
● Traits
● Functional Programming
– curry
Spock
Grails
Gradle grooscript
The Groovy Ecosystem
and many others….
Griffon
Gradle
Imperative, not declarative
Relies on plugins
Tasks
Vanilla groovy
Easy to build plugins
Easy to do sub-projects
Spock
Built on JUnit
Somewhat enhanced groovy
“test names can be any string”
given: when: then: expect:
Built-in mocking
Table-syntax for provided test data
Pretty assert output
Thanks!
Adam L. Davis
“Learning Groovy”
github.com/adamldavis
/2017-gr8conf-learning-groovy
adamldavis.com
@adamldavis
groocss.org
@groocss
How? Gedit + https://github.com/aeischeid/gedit-grails-bundle

More Related Content

What's hot

Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
Aniruddha Chakrabarti
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
Manav Prasad
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
Slawomir Dorzak
 
Python Workshop. LUG Maniapl
Python Workshop. LUG ManiaplPython Workshop. LUG Maniapl
Python Workshop. LUG Maniapl
Ankur Shrivastava
 
Project Lambda - Closures after all?
Project Lambda - Closures after all?Project Lambda - Closures after all?
Project Lambda - Closures after all?
Andreas Enbohm
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Defect prevention software
Defect prevention softwareDefect prevention software
Defect prevention software
Zarko Acimovic
 
An Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented ProgrammingAn Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented Programming
Krzysztof Opałka
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
Daniele Esposti
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
Kamil Toman
 
Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)
Ken Kousen
 
Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
Muhammad Muzammal
 
Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)
Adam Mukharil Bachtiar
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
Mihai Todor
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift Awesome
Sokna Ly
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
ForwardBlog Enewzletter
 
JVM Performance Magic Tricks
JVM Performance Magic TricksJVM Performance Magic Tricks
JVM Performance Magic Tricks
Takipi
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
Sri Harsha Pamu
 
Using R in remote computer clusters
Using R in remote computer clustersUsing R in remote computer clusters
Using R in remote computer clusters
Burak Himmetoglu
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 

What's hot (20)

Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
 
Python Workshop. LUG Maniapl
Python Workshop. LUG ManiaplPython Workshop. LUG Maniapl
Python Workshop. LUG Maniapl
 
Project Lambda - Closures after all?
Project Lambda - Closures after all?Project Lambda - Closures after all?
Project Lambda - Closures after all?
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Defect prevention software
Defect prevention softwareDefect prevention software
Defect prevention software
 
An Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented ProgrammingAn Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented Programming
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)
 
Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
 
Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift Awesome
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
 
JVM Performance Magic Tricks
JVM Performance Magic TricksJVM Performance Magic Tricks
JVM Performance Magic Tricks
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Using R in remote computer clusters
Using R in remote computer clustersUsing R in remote computer clusters
Using R in remote computer clusters
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 

Similar to Learning groovy 1: half day workshop

Groovy.pptx
Groovy.pptxGroovy.pptx
Groovy.pptx
Giancarlo Frison
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
Sergey Pichkurov
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
elliando dias
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
Jonathan Felch
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
Matthew Ahrens
 
Groovy to gradle
Groovy to gradleGroovy to gradle
Groovy to gradle
Geng-Dian Huang
 
Getting Groovy
Getting GroovyGetting Groovy
Getting Groovy
Adam Davis
 
Groovy
GroovyGroovy
Groovy
Zen Urban
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
Bindesh Vijayan
 
Clojure
ClojureClojure
Clojure
Rohit Vaidya
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talk
desistartups
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
Liviu Tudor
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
Rodolfo Carvalho
 
Apache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouApache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and You
Andres Almiray
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
Marcin Grzejszczak
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
Isuru Samaraweera
 
07. haskell Membership
07. haskell Membership07. haskell Membership
07. haskell Membership
Sebastian Rettig
 
Dart workshop
Dart workshopDart workshop
Dart workshop
Vishnu Suresh
 
PigHive.pptx
PigHive.pptxPigHive.pptx
PigHive.pptx
DenizDural2
 
An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java Developers
Kostas Saidis
 

Similar to Learning groovy 1: half day workshop (20)

Groovy.pptx
Groovy.pptxGroovy.pptx
Groovy.pptx
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
 
Groovy to gradle
Groovy to gradleGroovy to gradle
Groovy to gradle
 
Getting Groovy
Getting GroovyGetting Groovy
Getting Groovy
 
Groovy
GroovyGroovy
Groovy
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
 
Clojure
ClojureClojure
Clojure
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talk
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Apache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouApache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and You
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
 
07. haskell Membership
07. haskell Membership07. haskell Membership
07. haskell Membership
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
PigHive.pptx
PigHive.pptxPigHive.pptx
PigHive.pptx
 
An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java Developers
 

Recently uploaded

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 

Recently uploaded (20)

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 

Learning groovy 1: half day workshop

  • 1. Learning Groovy (in 3 hours)! Adam L. Davis The Solution Design Group, Inc. Author of “Learning Groovy” (& What’s New in Java 8 & others) 14 years Java Dev. github.com/adamldavis /2017-gr8conf-learning-groovy
  • 2. Java ~ Brian Goetz (Java Language Architect)
  • 6. ● Dynamic or Static ● (@CompileStatic @TypeChecked) ● As fast as Java (with static & indy) ● Meta-programming ● Optional semi-colons ● Optional parentheses ● Short-hand for Lists and Maps ● Automatic getters and setters ● A better switch ● Groovy GDK… Groovy 2.4 Features
  • 7. ● Closures ● Currying ● Method references ● Map/Filter/Reduce as collect, findAll, inject ● Internal iterating using each ● Operator Overloading (+ - * % / …) ● methodMissing and propertyMissing ● AST Transformations ● Traits … Groovy 2.4 Features (cont.)
  • 8. Starting Out Option 1: Using sdkman.io – sdk install groovy 2.4.9 Option 2: Download from groovy-lang.org – Alter your PATH ● Export PATH=$PATH:/usr/lib/groovy/bin ● Option 3: Mac – see http://groovy-lang.org/install.html ● Option π: Windows – see https://github.com/groovy/groovy-windows-installer Then: $ groovyConsole IntelliJ IDEA or NetBeans
  • 9. Dynamic typing ● def keyword ● Parameters’ typing optional ● Possible to mock using a map – def dog = [bark: { println ‘woof’ }] ● Using @TypeChecked or @CompileStatic you can make Groovy statically typed in some classes
  • 10. Groovy Strings ● ‘normal string’ ● “groovy string can contain $variables” ● “can also do expressions ${x + 1}” ● Use triple quote to start/end multi-line strings ‘’’ This is a Multi-line String ‘’’
  • 11. Math, Groovy Truth, and Equals ● Numbers are BigDecimal by default not Double – 3.14 is a BigDecimal – 3.14d is a Double ● Groovy truth: null, “”, [], 0 are false – if (!thing) println “thing was null” – if (!str) println “str was empty” ● Groovy == means .equals – For identity use a.is(b)
  • 12. Property Access ● Everything is public by default ● Every field has a getter and setter by default ● Gotcha’s – Map access – String.class->String ● Property access automatically uses getters and setters ● foo.bar == foo.getBar() ● foo.bar = 2 == foo.setBar(2)
  • 13. Lists and Maps ● def emptyList = [ ] ● def emptyMap = [:] ● def numList = [1,2,3] ● def strMap = [cars: 1, boats: 2, planes: 3] ● def varMap = [(var1): 1, (var2): 2, (var3): 3]
  • 14. Maps Continued... ● def map = [cars: 1, boats: 2, planes: 3] ● String key access: map.cars ● OR map[‘cars’] ● Also works for modifying: – map.cars = 42 – map[‘cars’] = 42
  • 16. A better switch ● Switch can use types, lists, ranges, patterns… Switch (x) { case Map: println “was a map”; break case [4,5,6]: println “was 4, 5 or 6”; break case 0..20: println “was 0 to 20”; break case ~/w+/: println “ was a word”; break case “hello”: println x; break case BigDecimal: println “was a BigDecimal”
  • 17. Groovy GDK ● Adds methods to everything! Adds its own classes... ● Collections: sort, findAll, collect, inject, each,… ● IO: toFile(), text, bytes, withReader, URL.content ● Ranges: x..y, x..<y – GetAt syntax for Strings and Lists: ● text[0..4] == text.substring(0,5) ● Utilities: ConfigSlurper, Expando, ObservableList/Map/Set
  • 18. Safe dereference & Elvis operator ● Safe dereference ?. – String name = person?.name – Java: person == null ? null : person.getName() ● Elvis operator ?: – String name = person?.name ?: “Bob” – Java: if (name == null) name = “Bob”
  • 19. Closures ● Closure: “a self-containing method” (like Lambda exp.) – def say = { x -> println x } – say(‘hello gr8conf’) – def say = { println it } – def adder = { x, y -> x + y } ● Closures have several implicit variables: – it - If the closure has one argument – this - Refers to the enclosing class – owner - The same as this unless it is enclosed in another closure. – delegate - Usually the same as owner but you can change it (this allows the methods of delegate to be in scope).
  • 20. Closures Continued... ● When used as last parameter, closure can go outside parentheses – methodCalled(param1, param2) { closureHere() } – methodWithOnlyClosure { closureHere() }
  • 21. Regex Pattern Matching ● Regex = regular expressions ● Within forward slashes / is a regex – You don’t need to use double ● =~ for matching anywhere within a string – if (text =~ /d+/) println “there was a number in it” ● ==~ for matching the whole string – if (email ==~ /[w.]+@[w.]+/) println “it’s an email”
  • 22. Meta-programming ● Every class and instance has a metaClass ● String.metaClass.upper = { delegate.toUpperCase() } – “foo”.upper() == “FOO” ● Traits can be used as mixins ● Maps can be cast to actual types using as [bark: {println “Woof!”}] as Dog
  • 24. Other Surprises for Java Devs ● Default values for method parameters ● groovy.transform.* – @EqualsAndHashCode – @TupleConstructor – @ToString – @Canonical ● Generics not enforced by default
  • 25. Advanced Groovy ● Groovy Design Patterns – Strategy pattern – Categories – Method caching ● DSLs – Domain Specific Languages – Operator Overloading ● Traits ● Functional Programming – curry
  • 26. Spock Grails Gradle grooscript The Groovy Ecosystem and many others…. Griffon
  • 27. Gradle Imperative, not declarative Relies on plugins Tasks Vanilla groovy Easy to build plugins Easy to do sub-projects
  • 28. Spock Built on JUnit Somewhat enhanced groovy “test names can be any string” given: when: then: expect: Built-in mocking Table-syntax for provided test data Pretty assert output
  • 29. Thanks! Adam L. Davis “Learning Groovy” github.com/adamldavis /2017-gr8conf-learning-groovy adamldavis.com @adamldavis groocss.org @groocss How? Gedit + https://github.com/aeischeid/gedit-grails-bundle