Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
@melix
melix / delegatesto.groovy
Created October 2, 2012 18:22
Statically compiled builder
class Xml {
boolean called = false
void bar() { called = true }
void foo(@DelegatesTo(Xml)Closure cl) { cl.delegate=this;cl() }
}
def mylist = [1]
def xml = new Xml()
xml.foo {
mylist.each { bar() }
}
@melix
melix / delegatesto2.groovy
Created October 17, 2012 02:45
DelegatesTo parameter
class Foo {
def foo() { println 'Called foo' }
}
@groovy.transform.CompileStatic
def with(Object target, @DelegatesTo(parameter='target') Closure arg) {
arg.delegate = target
arg()
}
@melix
melix / xkcd_constraint.groovy
Created December 5, 2012 17:27
Constraint programming + @CompileStatic
@GrabResolver('http://www.emn.fr/z-info/choco-repo/mvn/repository/')
@Grab('choco:choco-solver:2.1.5')
import static choco.Choco.*
import choco.cp.model.CPModel
import choco.cp.solver.CPSolver
import choco.kernel.model.variables.integer.IntegerVariable
@groovy.transform.CompileStatic
def solve(Map<String,Integer> menu, int sum) {
def m = new CPModel()
@melix
melix / contribs.groovy
Created December 17, 2012 16:30
Groovy contributors
def parser = new groovy.json.JsonSlurper()
def contribs = parser.parseText(new URL('https://api.github.com/repos/groovy/groovy-core/contributors').text)
def stats = contribs.inject([sum:0,contribs:[:]]) { map, val ->
map.contribs[val.login] = val.contributions
map.sum += val.contributions
map
}
stats.contribs.each {
println "${it.key.padRight(16)} = $it.value (${(int)(100*it.value/stats.sum)}%)"
@melix
melix / Main.groovy
Created January 30, 2013 18:25
First experiment with @bytecode and invokedynamic
import groovy.transform.CompileStatic
import groovyx.ast.bytecode.Bytecode
import java.lang.invoke.*;
import java.lang.invoke.MethodHandles.Lookup;
import static groovyjarjarasm.asm.Opcodes.*
import static java.lang.invoke.MethodHandles.*
public static MethodHandle findMethod(Lookup lookup, String name) {
lookup.findVirtual(Main, name, MethodType.methodType(int))
}
@melix
melix / Functions.groovy
Created February 18, 2013 16:21
Script imports in groovy
static void greet(String name) { println "Hello, $name!" }
@melix
melix / gist:5165645
Created March 14, 2013 22:00
Why runtime checks are required for reified generics
First, create:
foo/Echo.ceylon:
class Echo() {
shared void echo(Array<String> list) { print(list); }
}
foo/Echo2.ceylon
import foo {Echo}
@melix
melix / dynamic-properties.groovy
Created April 2, 2013 08:44
Dynamic properties access in Groovy
@Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1')
class Person {
String name
int age
}
def p = new Person(name:'foo', age:10)
def pName = 'name'
def props = p.properties
@melix
melix / Benchmark.java
Last active December 15, 2015 16:49
Micro-benchmarks about indy
/*
* Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@melix
melix / SynchronizedRemover.groovy
Created April 5, 2013 13:21
Prevents usage of synchronized methods
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.syntax.SyntaxException
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.classgen.GeneratorContext
class SynchronizedRemover extends org.codehaus.groovy.control.customizers.CompilationCustomizer {
SynchronizedRemover() {
super(org.codehaus.groovy.control.CompilePhase.CONVERSION)
}