Skip to content

Instantly share code, notes, and snippets.

View dmahapatro's full-sized avatar

Dhiraj Mahapatro dmahapatro

View GitHub Profile
@dmahapatro
dmahapatro / YamlToConfigObject.groovy
Created February 4, 2015 17:04
YAML to Groovy Config
// 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()
@dmahapatro
dmahapatro / DeepMergeMap.groovy
Last active January 7, 2016 13:37
Deep Merge Groovy Map
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 ] ]
@dmahapatro
dmahapatro / Prove4Equals5.groovy
Last active January 4, 2016 18:19
Prove two adjacent numbers are equal.
//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
@dmahapatro
dmahapatro / AddPrimes.groovy
Last active January 1, 2016 22:29
Performance hit using groovy-stream.
//Project Euler # 10
//http://projecteuler.net/problem=10
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
@Grab( 'com.bloidonia:groovy-stream:0.6.2' )
import groovy.stream.Stream
import groovyx.gbench.Benchmark
def usingStream(){
@dmahapatro
dmahapatro / 0_reuse_code.js
Last active August 29, 2015 14:10
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
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()}"
@dmahapatro
dmahapatro / PerformanceBenchmarkFindVersusInList.groovy
Last active August 29, 2015 14:03
Benchmark performance between find and in list to match a key-value from a map present in list.
@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']]
@dmahapatro
dmahapatro / TracingInterceptorSample.groovy
Last active August 29, 2015 14:00
Debugging method calls using TracingInterceptor
/*
* 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() }
@dmahapatro
dmahapatro / TraitASTSample.groovy
Last active August 29, 2015 14:00
Use of @trait AST in Groovy without creating a trait
/* 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)" }
}
@dmahapatro
dmahapatro / BuilderASTStrategies.groovy
Last active August 29, 2015 14:00
@builder AST Strategies sample
/* 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