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
Map.metaClass.merge = { Map rhs -> | |
def lhs = delegate // or delegate.clone() to make delegate immutable | |
rhs.each { k, v -> lhs[k] = lhs[k] in Map ? lhs[k].merge(v) : (lhs[k] != null ? lhs[k] && v : v) } | |
lhs | |
} | |
def a = [ foo: [ foo: true, bar: true ] ] | |
def b = [ foo: [ bar: false, baz: true ] ] | |
assert a.merge(b) == [ foo: [ foo: true, bar: false, baz: true ] ] |
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
// Run in Grails context inside grails console | |
// Below will be helpful to create "application.groovy" from "application.yml" in Grails 3 | |
// which will be an easy porting experiance from Grails 2.* to Grails 3 | |
// In future, this can be added to a grails command and/or added to a Grails profile. | |
import org.springframework.beans.factory.config.YamlMapFactoryBean | |
import org.springframework.core.io.ClassPathResource | |
import org.springframework.core.io.Resource | |
YamlMapFactoryBean factory = new YamlMapFactoryBean() |
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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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
def myString = """ | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum | |
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
""" | |
def myStringAsList = myString.split() | |
// Word Count | |
println "Word Count -> ${myStringAsList.size()}" |
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
@Grab(group='com.googlecode.gbench', module='gbench', version='11.07.05') | |
def r = benchmark { | |
'findTestWithSmallerListSize' { | |
def myList = [[first: 'John', last: 'test']] | |
myList.find { it.first == 'John' } ? true : false | |
} | |
'inListTestWithSmallerListSize' { | |
def myList = [[first: 'John', last: 'test']] |
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
/* | |
* Nice way to debug method calls by intercepting all method invocations | |
* on a GroovyObject using TracingInterceptor. | |
*/ | |
class MyClass { | |
String name | |
def reverse() { name.reverse() } | |
def upperCase() { name.toUpperCase() } |
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
/* Using @Trait AST provided by Groovy 2.3 to convert a pre-existing class to a Trait */ | |
import groovy.transform.Trait | |
//Declare the class as a Trait by using the Trait | |
//without creating a new trait | |
@Trait | |
class FlyingAbility { | |
String fly() { "${this.name}: I can fly (wink)" } | |
} |
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
/* Sample use cases of @Builder AST which is added in Groovy 2.3 */ | |
import groovy.transform.builder.* | |
import groovy.transform.ToString | |
//DefaultStrategy | |
@ToString(includeNames=true) | |
@Builder(builderStrategy=DefaultStrategy) | |
class Person { | |
String name |
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 java.io.Closeable | |
import java.io.IOException | |
trait Closing { | |
def closeQuietly(){ | |
try{ | |
println "Closing" | |
this.close() | |
} catch (IOException e) { | |
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
//Prove 4 == 5 | |
def lhs = 4 | |
def rhs = 5 | |
assert -20 == -20 | |
assert 16 - 36 == 25 - 45 | |
assert lhs ** 2 - 36 == rhs ** 2 - 45 | |
assert lhs ** 2 - 2 * lhs * ( 9 / 2 ) == rhs ** 2 - 2 * rhs *( 9 / 2 ) | |
assert lhs ** 2 - 2 * lhs * ( 9 / 2 ) + ( 9 / 2 ) ** 2 == rhs ** 2 - 2 * rhs * ( 9 / 2 ) + ( 9 / 2 ) ** 2 |
NewerOlder