Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
@melix
melix / ast-matching.groovy
Created October 17, 2014 22:19
AST Pattern matching
// "macro" builds the AST of the corresponding block
def ast1 = macro { foo(a) + foo(a) }
def ast2 = macro { foo(a) + foo(foo(a)) }
// we build another AST which will be used as a pattern
def pattern = macro {
x + foo(x)
}.withConstraints {
// declare that x is not a variable, but a placeholder
placeholder x

Keybase proof

I hereby claim:

  • I am melix on github.
  • I am melix (https://keybase.io/melix) on keybase.
  • I have a public key whose fingerprint is 3312 24E1 D7BE 883D 16E8 A685 825C 06C8 27AF 6B66

To claim this, I am signing this object:

@melix
melix / fatfingers.groovy
Last active August 29, 2015 14:06
Fat fingers correction in Groovy
import org.codehaus.groovy.reflection.ClassInfo
import org.codehaus.groovy.runtime.MethodRankHelper
trait FatFingers {
def methodMissing(String name, args) {
def ci = ClassInfo.getClassInfo(this.class)
def methods = [*ci.metaClass.methods,*ci.metaClass.metaMethods]
def sugg = MethodRankHelper.rankMethods(name,args,methods)
if (sugg) {
sugg[0].invoke(this, args)
@melix
melix / build.gradle
Created September 4, 2014 11:19
Workaround for VerifyError in Android+Groovy builds
// add this to build.gradle
gradle.taskGraph.whenReady {
allprojects {
tasks.withType(GroovyCompile) { task ->
logger.lifecycle("Task $task")
task.groovyOptions.forkOptions.jvmArgs=['-noverify']
}
}
}
@melix
melix / Foo.groovy
Created July 22, 2014 12:47
Dump AST transformations
import groovy.transform.ASTTest
import groovy.transform.CompileStatic
import groovy.transform.Immutable
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.control.CompilePhase
@ASTTest(phase=CompilePhase.INSTRUCTION_SELECTION,value={
ClassNode cn = node
def config = cn.compileUnit.config
def cu = cn.compileUnit.classLoader.createCompilationUnit(config, null)
@melix
melix / infiniteloop.groovy
Created June 11, 2014 19:54
Infinite loop
import static Double.POSITIVE_INFINITY as ∞
∞.times {
println it
Thread.sleep(1000)
}
@melix
melix / Fluent.groovy
Created June 6, 2014 09:55
Fluent class for AsyncTask
package me.champeau.gr8confagenda.app;
import android.os.AsyncTask;
import groovy.lang.Closure;
/**
* An implementation of {@link android.os.AsyncTask} which makes it easy to deal with
* requests/callbacks using Groovy closures
*/
@melix
melix / async.groovy
Created May 27, 2014 17:13
Tired of AsyncTask in Android?
// Tired of AsyncTask in Android?
// Here's how you could write it in Groovy!
Fluent.async {
new URL('http://foo').text
}.then { text ->
textView.text = text
}
@melix
melix / hodorlang.groovy
Last active August 29, 2015 14:01
The Hodor DSL
import org.codehaus.groovy.control.CompilerConfiguration
/*
* Copyright 2003-2014 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
@melix
melix / caching.groovy
Created May 21, 2014 14:51
Caching with invokedynamic
import groovy.transform.CompileStatic
import java.lang.invoke.CallSite
import java.lang.invoke.ConstantCallSite
import java.lang.invoke.MethodHandle
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
import java.lang.invoke.SwitchPoint
import static groovyjarjarasm.asm.Opcodes.*