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. GROOVY AS A SCRIPTING
    LANGUAGE
    Created by /
    Jenn Strater @jennstrater

    View Slide

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

    View Slide

  3. View Slide

  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
    !
    "

    View Slide

  5. DATA TYPES
    Strings
    Functions
    Collections
    Lists
    Maps

    View Slide

  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 '
    '
    '

    View Slide

  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

    View Slide

  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

    View Slide

  9. COLLECTIONS

    View Slide

  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
    '
    ]

    View Slide

  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
    ]

    View Slide

  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
    ]

    View Slide

  13. LOOPS

    View Slide

  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
    ]

    View Slide

  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
    ]

    View Slide

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

    View Slide

  17. FILE PROCESING

    View Slide

  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
    !
    '
    )

    View Slide

  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
    !

    View Slide

  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!

    View Slide

  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/

    View Slide