October 3, 2015
by Jenn Strater @jennstraterCurrent
Professional
Experience
source: http://tjvantoll.com/2014/12/29/so-you-want-to-write-a-tech-book/Minneapolis, MN
Apache
To contribute: https://github.com/apache/incubator-groovy
http://groovy-lang.org/download.html
curl -s get.sdkman.io | bash
groovy>println "Hello, Open Source North!"
Hello, Open Source North!
===>null
int i = 10
println i.class.name
-> java.lang.Integer
Integer i = 20
println i.class.name
-> java.lang.Integer
when no type is specified or for a flexible type
def i = 10
println i * 2
-> 20
i = 'abc'
println i * 2
-> abcabc
(examples in console)
(examples in console)
List m = ['a', 'b', 2]
println m[2]
-> 2
m[2] = 3
println m
-> [a, b, 3]
m << 'c'
println m
-> [a, b, 3, c]
m.push(4)
-> [a, b, 3, c, 4]
m.pop()
-> 4
List m = ['a', 'b', 2]
m.multiply(2)
-> ['a', 'b', 2, 'a', 'b', 2]
m * 2
-> ['a', 'b', 2, 'a', 'b', 2]
m + 2
-> ['a', 'b', 2, 2]
(examples in console)
Map m = [val1: 'a', val2: 'b', val3: 2]
println m.val1
-> a
println m['val3']
-> 2
m.val4 = 3
println m
-> [val1:a, val2:b, val3: 2, val4: 3]
println m.keySet()
-> [val1, val2, val3, val4]
println m.values()
-> [a, b, 2, 3]
println (1..10)
-> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
println (1..<10)
-> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Integer timesTwo(x) {
x + x
}
println timesTwo(10)
-> 20
def result = timesTwo 10
println result
-> 20
Code as Data
(to the console)
Closure c = { name, year = 2015 ->
println "Hello, $name $year!"
}
c.call('Open Source North')
-> "Hello, Open Source North 2015!"
c('Open Source North', 2016)
-> "Hello, Open Source North 2016"
List dl = []
for(Integer i = 1; i < 4; i++){
dl.add(i * 2)
}
println dl
-> [2, 4, 6]
List dl = []
for(i in [1,2,3]) {
dl << i * 2
}
println dl
-> [2, 4, 6]
List sl = [1, 2, 3]
List dl = []
sl.each { it ->
dl << it * 2
}
println dl
-> [2, 4, 6]
Map m = [va1: 1, var2: 2, var3: 3]
List dl = m.collect { key, value ->
value * 2
}
println dl
-> [2, 4, 6]
(1..3).multiply(2)
-> [1, 2, 3, 1, 2, 3]
(1..3)*.multiply(2)
-> [2, 4, 6]
Print the sum of the numbers from 1 to 10
option 1:Integer answer = 0 for(i in [1,2,3,4,5,6,7,8,9,10]) { answer += i } println answer
option 2:Integer answer = 0 (1..10).each { answer = answer + it } println answer
bonus answer:println (1..10).sum()
False values
== verifies value
is verifies object identity
List numbers = [0,1,2,3]
numbers.each {
if(it) {
println it
}
}
-> 1
-> 2
-> 3
x ? return x : return 'something else'
x ?: 'something else'
myMap?.propertyA?.propertyB
Map m = [val1: 2, val2: 4, val3: 6]
m.find { it.value % 2 == 0 && it.value % 3 != 0 }
-> val1=2
m.findAll {it.value % 2 == 0 && it.value % 3 != 0 }
-> [val1:2, val2:4]
Map m = [val1: 2, val2: 4, val3: 6]
m.any { it.value % 2 == 0 }
-> true
m.every { it.value % 3 == 0 }
-> false
class Person {
String first
String last
}
Person me = new Person(first: 'Jenn', last: 'Strater')
println me.first
println me.last
-- add String company to class --
me.company = 'Object Partners'
println me.properties
-> [class:class Person, first:Jenn, company:Object Partners, last:Strater]
class Person {
String first
String last
String getFullName() {
this.first + ' ' + this.last
}
}
Person me = new Person(first: 'Jenn', last: 'Strater')
println me.fullName
-> 'Jenn Strater'
(save previous script as people.groovy)
groovy people.groovy
-> Jenn Strater
println doubledValue(10)
def doubledValue(x) {
x * 2
}
def myFile = new File('foo.txt')
myFile.write 'Hello, \n'
myFile.append('Open Source North!')
foo.txt
Hello,
Open Source North!
def myFile = new File('foo.txt')
myFile.eachLine { line ->
def processedLine = line.replaceAll('Hello', 'Hi')
println processedLine
}
-> Hi,
-> Open Source North!
(copy to console)
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.RESTClient
def http = new RESTClient("http://www.gr8ladies.org")
http.get(path: '/api/companies') { resp, reader ->
reader.sort { -it.percentageTotalWomen } // sort by percentage women in descending order
reader[0..4].each{ // only the first 5
if(it.totalEmployees) { // if the value of total employees is not 0 (to eliminate companies that didn't report that field)
println 'name:' + it.name
println 'country:' + it.country
println 'total employees: ' + it.totalEmployees
println 'total men: '+ it.totalMen
println 'total women: '+ it.totalWomen + '\n'
}
}
}
return null // don't print the entire result at the end
(copy to console)
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.RESTClient
def http = new RESTClient("http://api.icndb.com")
http.get(path: '/jokes/random/5', query: [exclude: 'explicit']) { resp, reader ->
reader.value.joke.each{
println it + '\n'
}
}
jenn@gr8ladies.org