Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
@melix
melix / build.gradle
Last active August 29, 2015 14:15
Délourdage en Gradle
apply plugin:'java'
repositories {
jcenter()
}
configurations {
manu // une conf qui servira à récupérer la dep sans polluer le classpath de compilation
}
:connectedAndroidTest
Tests on Galaxy_Nexus(AVD) - 4.2 failed: Unable to find instrumentation info for: ComponentInfo{grooid.app.test/android.support.test.runner.AndroidJUnitRunner}
com.android.builder.testing.ConnectedDevice > hasTests[Galaxy_Nexus(AVD) - 4.2] FAILED
No tests found.
:connectedAndroidTest FAILED
@melix
melix / cli-friendly.groovy
Created January 2, 2015 16:50
Improving CLI mode for Groovy
class CLICategory {
static Iterator getLines(String f) { new File(f).iterator() }
static int getLength(String s) { s.length() }
static def max(Iterator c,String property) { c.max { it."$property" } }
}
def propertyMissing(String p) { "$p" }
use (CLICategory) {
'quotes.txt'.lines.max length
@melix
melix / Fibonenchmark
Last active August 29, 2015 14:10
Fibonacci Benchmark
# Run complete. Total time: 00:34:14
Benchmark Mode Samples Score Error Units
o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_cs avgt 30 1.961 ± 0.055 ms/op
o.g.m.f.FibonacciMicroBenchmark.baseline_java_30 avgt 30 2.004 ± 0.084 ms/op
o.g.m.f.FibonacciMicroBenchmark.baseline_java_boxing_30 avgt 30 5.504 ± 0.036 ms/op
o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_30 avgt 30 5.733 ± 0.024 ms/op
o.g.m.f.FibonacciMicroBenchmark.groovy_indy_30 avgt 30 5.903 ± 0.033 ms/op
o.g.m.f.FibonacciMicroBenchmark.golo_30 avgt 30 6.545 ± 0.060 ms/op
o.g.m.f.FibonacciMicroBenchmark.nashorn_30 avgt 30 7.812 ± 0.061 ms/op
@melix
melix / Dockerfile
Last active June 2, 2020 01:37
Dockerfile for Groovy
################################################
# Dockerfile to run Groovy containers
# Based on Java 8 image
################################################
FROM java:8u40-jdk
MAINTAINER Cédric Champeau
# Install GVM
@melix
melix / astrelax.groovy
Created November 12, 2014 10:01
AST pattern matching with relaxed constraints 2
void testInlineMacroCombinationWithConstraints() {
use(ASTMatcher) {
def ast1 = macro { a + b }
def ast2 = macro { b + b }
def ast3 = macro { b + c }
def ast4 = macro { b - b }
def pattern = macro {
$v { macro { a }.withConstraints { placeholder a } } + b
}.withConstraints {
anyToken()
@melix
melix / matchtest.groovy
Created November 12, 2014 07:27
AST pattern matching with relaxed constraints
void testRelaxedBinaryExpressionWithConstrainedToken() {
use(ASTMatcher) {
def ast1 = macro { a + b }
def ast2 = macro { a - b }
def ast3 = macro { a * b }
def ast4 = macro { a + c }
def pattern = macro {
a + b
}.withConstraints {
token {
@melix
melix / ratpack-delegation.groovy
Last active August 29, 2015 14:08
DelegatesTo any type in Groovy 2.4+
trait Configurable<ConfigObject> {
ConfigObject configObject
void configure(Closure<Void> configSpec) {
configSpec.resolveStrategy = Closure.DELEGATE_FIRST
configSpec.delegate = configObject
configSpec()
}
}
@melix
melix / configurable.groovy
Created November 2, 2014 10:35
DelegatesTo + generic
import groovy.transform.CompileStatic
public <T> T configure(@DelegatesTo.Target Class<T> clazz, @DelegatesTo(genericTypeIndex = 0) Closure<Void> conf) {
def result = clazz.newInstance()
result.with(conf)
result
}
class Person {
String name
@melix
melix / pieceofcake.groovy
Created October 27, 2014 14:23
Piece of cake like pattern in Groovy
import groovy.util.ProxyGenerator
import groovy.transform.CompileStatic
// just for tests, can be replaed by any mocking framework
class Mock {
static <T> T f(Class<T> clazz, Closure cl) {
ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(cl, clazz)
}
}