Save 37% off PRO during our Black Friday Sale! »

Groovy as a Scripting Language

Groovy as a Scripting Language

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

1f28a0c1988421be3268026bd6bb6f49?s=128

jlstrater

June 28, 2014
Tweet

Transcript

  1. GROOVY AS A SCRIPTING LANGUAGE Created by / Jenn Strater

    @jennstrater
  2. WHAT'S GROOVY? Groovy is a dynamic compiled language for the

    Java Virtual Machine (JVM)
  3. None
  4. 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 ! "
  5. DATA TYPES Strings Functions Collections Lists Maps

  6. 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 ' ' '
  7. 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
  8. 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
  9. COLLECTIONS

  10. 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 ' ]
  11. SPECIAL FUNCTIONS Spread Dot m y L i s t

    * . m u l t i p l y ( 2 ) - - > [ ' a a ' , ' b b ' , 4 ]
  12. 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 ]
  13. LOOPS

  14. 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 ]
  15. 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 ]
  16. Ranges ( 1 . . 3 ) . e a

    c h { p r i n t l n i t * 2 } - - > 2 - - > 4 - - > 6
  17. FILE PROCESING

  18. 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 ! ' )
  19. 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 !
  20. 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!
  21. 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/