Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Groovy as a Scripting Language

Groovy as a Scripting Language

Gr8Ladies Gr8Workshop Sesion1: Groovy as a Scripting Language
June 28, 2014

jlstrater

June 28, 2014
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. 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 ! "
  2. 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 ' ' '
  3. 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
  4. 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
  5. 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 ' ]
  6. SPECIAL FUNCTIONS Spread Dot m y L i s t

    * . m u l t i p l y ( 2 ) - - > [ ' a a ' , ' b b ' , 4 ]
  7. 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 ]
  8. 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 ]
  9. 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 ]
  10. Ranges ( 1 . . 3 ) . e a

    c h { p r i n t l n i t * 2 } - - > 2 - - > 4 - - > 6
  11. 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 ! ' )
  12. 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 !
  13. 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!