Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
GROOVY AS A SCRIPTING
LANGUAGECreatedby /JennStrater @jennstrater
WHAT'S GROOVY?
Groovyis adynamic compiled language for the JavaVirtual
Machine (JVM)
HELLO WORLD
IN JAVA:
publicclassMain
{
publicstaticvoidmain(String[]arguments){
System.out.println("HelloWorld!");
}
}
IN ...
DATA TYPES
Strings
Functions
Collections
Lists
Maps
STRINGS
Single Quotes
'Hello,world!'
Double Quotes --GString
"Hello,$name"
Multi-line Strings
'''This
is
a
multi-line
stri...
STRING MANIPULATION
Split
defmyString='I<3Groovy'
myString.tokenize('<3')
-->[I,Groovy]
Join
defmyListOfStrings=['I','<3',...
CLOSURES
defa={params->println(params*2)}
[1,2,3].eacha
-->2
-->4
-->6
a(1)
-->2
COLLECTIONS
Lists
defmyList=['a','b',2]
myList.multiply(2)
-->['a','b','2','a','b','2']
SPECIAL FUNCTIONS
Spread Dot
myList*.multiply(2)
-->['aa','bb',4]
MAPS
defmyMap=[val1:'a',val2:'b',val3:'b']
myMap.values()
-->['a','b',2]
myMap.find{it.value=='b'}
-->val2=b
myMap.findAll...
LOOPS
Each
defmyList=[1,2,3]
defresult=[]
myList.each{
result<<it*2
}
printlnresult
-->[2,4,6]
Collect
defmyList=[1,2,3]
defresult=myList.collect{item->
item.multiply(2)
}
printlnresult
-->[2,4,6]
Ranges
(1..3).each{
printlnit*2
}
-->2
-->4
-->6
FILE PROCESING
CREATING & WRITING TO FILES
defmyFile=newFile('foo.txt')
myFile.write'hello,world!!n'
myFile.append('andhellouniverse!')
READING FILES
defmyFile=newFile('foo.txt')
myFile.eachLine{line->
defprocessedLine=line.replaceAll('hello','hi')
printlnpr...
THINGS TO REMEMBER
Typingvariables is optional
Some syntax is optional
semi-colons
parenthesis around function parameters
...
EXERCISES (HANDOUT)
RESOURCES
http://beta.groovy-lang.org/docs/groovy-
2.3.1/html/documentation/#_introduction
http://beta...
Groovy as a scripting language
Upcoming SlideShare
Loading in …5
×

Groovy as a scripting language

328 views

Published on

New To Groovy - Session 1 for Gr8Ladies Gr8Workshop

Published in: Software, Technology
  • Be the first to comment

Groovy as a scripting language

  1. 1. GROOVY AS A SCRIPTING LANGUAGECreatedby /JennStrater @jennstrater
  2. 2. WHAT'S GROOVY? Groovyis adynamic compiled language for the JavaVirtual Machine (JVM)
  3. 3. HELLO WORLD IN JAVA: publicclassMain { publicstaticvoidmain(String[]arguments){ System.out.println("HelloWorld!"); } } IN GROOVY: println"Hello,World!"
  4. 4. DATA TYPES Strings Functions Collections Lists Maps
  5. 5. STRINGS Single Quotes 'Hello,world!' Double Quotes --GString "Hello,$name" Multi-line Strings '''This is a multi-line string'''
  6. 6. STRING MANIPULATION Split defmyString='I<3Groovy' myString.tokenize('<3') -->[I,Groovy] Join defmyListOfStrings=['I','<3','Groovy'] myListOfStrings.join('') -->I<3Groovy
  7. 7. CLOSURES defa={params->println(params*2)} [1,2,3].eacha -->2 -->4 -->6 a(1) -->2
  8. 8. COLLECTIONS
  9. 9. Lists defmyList=['a','b',2] myList.multiply(2) -->['a','b','2','a','b','2']
  10. 10. SPECIAL FUNCTIONS Spread Dot myList*.multiply(2) -->['aa','bb',4]
  11. 11. MAPS defmyMap=[val1:'a',val2:'b',val3:'b'] myMap.values() -->['a','b',2] myMap.find{it.value=='b'} -->val2=b myMap.findAll{it.value=='b'} -->[val2:b,val3:b]
  12. 12. LOOPS
  13. 13. Each defmyList=[1,2,3] defresult=[] myList.each{ result<<it*2 } printlnresult -->[2,4,6]
  14. 14. Collect defmyList=[1,2,3] defresult=myList.collect{item-> item.multiply(2) } printlnresult -->[2,4,6]
  15. 15. Ranges (1..3).each{ printlnit*2 } -->2 -->4 -->6
  16. 16. FILE PROCESING
  17. 17. CREATING & WRITING TO FILES defmyFile=newFile('foo.txt') myFile.write'hello,world!!n' myFile.append('andhellouniverse!')
  18. 18. READING FILES defmyFile=newFile('foo.txt') myFile.eachLine{line-> defprocessedLine=line.replaceAll('hello','hi') printlnprocessedLine } -->hi,world! -->andhiuniverse!
  19. 19. THINGS TO REMEMBER Typingvariables is optional Some syntax is optional semi-colons parenthesis around function parameters explicitreturn statements The lastoperation completed is the defaultreturn value There is more than one wayto do almosteverything!
  20. 20. EXERCISES (HANDOUT) RESOURCES http://beta.groovy-lang.org/docs/groovy- 2.3.1/html/documentation/#_introduction http://beta.groovy-lang.org/docs/latest/html/groovy-jdk/

×