Slide 1

Slide 1 text

GROOVY AS A SCRIPTING LANGUAGE Created by / Jenn Strater @jennstrater

Slide 2

Slide 2 text

WHAT'S GROOVY? Groovy is a dynamic compiled language for the Java Virtual Machine (JVM)

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

HELLO WORLD IN JAVA: p u b l i c c l a s s M a i n { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g u m e n t s ) { S y s t e m . o u t . p r i n t l n ( " H e l l o W o r l d ! " ) ; } } IN GROOVY: p r i n t l n " H e l l o , W o r l d ! "

Slide 5

Slide 5 text

DATA TYPES Strings Functions Collections Lists Maps

Slide 6

Slide 6 text

STRINGS Single Quotes ' H e l l o , w o r l d ! ' Double Quotes -- GString " H e l l o , $ n a m e " Multi-line Strings ' ' ' T h i s i s a m u l t i - l i n e s t r i n g ' ' '

Slide 7

Slide 7 text

STRING MANIPULATION Split d e f m y S t r i n g = ' I < 3 G r o o v y ' m y S t r i n g . t o k e n i z e ( ' < 3 ' ) - - > [ I , G r o o v y ] Join d e f m y L i s t O f S t r i n g s = [ ' I ' , ' < 3 ' , ' G r o o v y ' ] m y L i s t O f S t r i n g s . j o i n ( ' ' ) - - > I < 3 G r o o v y

Slide 8

Slide 8 text

CLOSURES d e f a = { p a r a m s - > p r i n t l n ( p a r a m s * 2 ) } [ 1 , 2 , 3 ] . e a c h a - - > 2 - - > 4 - - > 6 a ( 1 ) - - > 2

Slide 9

Slide 9 text

COLLECTIONS

Slide 10

Slide 10 text

Lists d e f m y L i s t = [ ' a ' , ' b ' , 2 ] m y L i s t . m u l t i p l y ( 2 ) - - > [ ' a ' , ' b ' , ' 2 ' , ' a ' , ' b ' , ' 2 ' ]

Slide 11

Slide 11 text

SPECIAL FUNCTIONS Spread Dot m y L i s t * . m u l t i p l y ( 2 ) - - > [ ' a a ' , ' b b ' , 4 ]

Slide 12

Slide 12 text

MAPS d e f m y M a p = [ v a l 1 : ' a ' , v a l 2 : ' b ' , v a l 3 : ' b ' ] m y M a p . v a l u e s ( ) - - > [ ' a ' , ' b ' , 2 ] m y M a p . f i n d { i t . v a l u e = = ' b ' } - - > v a l 2 = b m y M a p . f i n d A l l { i t . v a l u e = = ' b ' } - - > [ v a l 2 : b , v a l 3 : b ]

Slide 13

Slide 13 text

LOOPS

Slide 14

Slide 14 text

Each d e f m y L i s t = [ 1 , 2 , 3 ] d e f r e s u l t = [ ] m y L i s t . e a c h { r e s u l t < < i t * 2 } p r i n t l n r e s u l t - - > [ 2 , 4 , 6 ]

Slide 15

Slide 15 text

Collect d e f m y L i s t = [ 1 , 2 , 3 ] d e f r e s u l t = m y L i s t . c o l l e c t { i t e m - > i t e m . m u l t i p l y ( 2 ) } p r i n t l n r e s u l t - - > [ 2 , 4 , 6 ]

Slide 16

Slide 16 text

Ranges ( 1 . . 3 ) . e a c h { p r i n t l n i t * 2 } - - > 2 - - > 4 - - > 6

Slide 17

Slide 17 text

FILE PROCESING

Slide 18

Slide 18 text

CREATING & WRITING TO FILES d e f m y F i l e = n e w F i l e ( ' f o o . t x t ' ) m y F i l e . w r i t e ' h e l l o , w o r l d ! ! \ n ' m y F i l e . a p p e n d ( ' a n d h e l l o u n i v e r s e ! ' )

Slide 19

Slide 19 text

READING FILES d e f m y F i l e = n e w F i l e ( ' f o o . t x t ' ) m y F i l e . e a c h L i n e { l i n e - > d e f p r o c e s s e d L i n e = l i n e . r e p l a c e A l l ( ' h e l l o ' , ' h i ' ) p r i n t l n p r o c e s s e d L i n e } - - > h i , w o r l d ! - - > a n d h i u n i v e r s e !

Slide 20

Slide 20 text

THINGS TO REMEMBER Typing variables is optional Some syntax is optional semi-colons parenthesis around function parameters explicit return statements The last operation completed is the default return value There is more than one way to do almost everything!

Slide 21

Slide 21 text

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/