Created
August 18, 2021 08:36
-
-
Save girisagar46/021042cac2beaeb710501122d658d0fd to your computer and use it in GitHub Desktop.
Groovy Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
println("Hello World") | |
println "Hello World" | |
println 'Hello World' | |
String name = 'Sagar' | |
println "Hello $name" | |
println "Hello ${name}" | |
x = 10 | |
println x | |
def date = new Date() | |
println "Today is $date" | |
println date.getClass().getName() | |
// POGO (Plain Old Groovy Object) | |
//@ToString(includeNames = true)// AST transformation | |
//@EqualsAndHashCode | |
//@TupleConstructor | |
@Canonical | |
class Person { | |
// If no access modifier is defined, fields are private by default | |
String first | |
String last | |
// We do not need return, it's implicit | |
/* | |
String toString() { | |
"$first $last" | |
} | |
*/ | |
} | |
Person qb = new Person() | |
// Groovy auto generates getter and setter | |
qb.setFirst("Sagar") | |
qb.last = "Giri" | |
// even if we do .last, it goes through generated getter and setter | |
println "${qb.getFirst()} ${qb.last}" | |
// Using constructor | |
Person qb1 = new Person(first: "Sagar", last: "Giri") | |
println "${qb1.getFirst()} ${qb1.last}" | |
println qb1.toString() | |
println qb1 == qb | |
Set people = [qb, qb1] | |
println people.size() | |
// We do not need to pass map in constructor because we've added TupleConstructor annotation | |
Person qb2 = new Person("Sagar", "Giri") // this is tuple constructor | |
println qb2.toString() | |
def nums = [1, 2, 3, 4, 5] | |
println nums | |
println nums.class.name | |
def numsLinked = [1, 2, 3, 4] as LinkedList | |
println numsLinked | |
println numsLinked.class.name | |
def numsSet = [1, 2, 3, 4] as SortedSet | |
println numsSet | |
println numsSet.class.name | |
for (n in numsSet) { | |
println n | |
} | |
numsSet.each { println it} // it is the default name | |
numsSet.each {n -> println n} | |
numsSet.eachWithIndex { int entry, int index -> println "nums[$index] = $entry" } | |
def doubles =[] | |
nums.each { doubles << it*2} | |
println doubles | |
println nums.collect{it * 2} // this is not modifying nums collection | |
def prefectures = ["Tokyo", "Chiba", "Hokkaido"] | |
def upperPrefecture = prefectures.collect{it.toUpperCase()} | |
println "upperPrefecture = $upperPrefecture" | |
// Groovy version of map->filter->reduce | |
def numbers = [1, 2, 3, 4, 5] | |
println numbers | |
println numbers.collect{it*2} // map | |
.findAll{it % 3 == 0} // filter | |
.sum() // reduce | |
def myMap = [a: 1, b: 2, c: 3] | |
println myMap | |
println myMap.keySet() | |
println myMap.values() | |
myMap.d = 4 | |
myMap["e"] = 5 | |
println myMap | |
myMap.each {entry -> println "$entry.key = $entry.value"} | |
myMap.each {k, v -> println "$k = $v"} | |
myMap.collect {k, v -> k * v} | |
String base = "https://maps.googleapis.com/maps/api/geocode/xml?" | |
def encodedAddress = ["2 Avenue de Lafayette", "Boston", "MA"].collect{ | |
URLEncoder.encode(it, 'UTF-8') | |
}.join(",") | |
String queryString = "address=$encodedAddress" | |
"$base$queryString".toURL().text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment