Skip to content

Instantly share code, notes, and snippets.

View mariogarcia's full-sized avatar

Mario Garcia mariogarcia

View GitHub Profile
@mariogarcia
mariogarcia / groovy-in-osgi-env
Created June 15, 2017 12:13 — forked from felixdo/groovy-in-osgi-env
run scripts from inside eclipse workbench
Bundle groovy = Platform.getBundle("org.codehaus.groovy");
final GroovyClassLoader transformLoader = new GroovyClassLoader(groovy.adapt(BundleWiring.class).getClassLoader());
GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader()) {
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
return new CompilationUnit(config, source, this, transformLoader, true, null, null);
}
};
Class<?> cl = loader.parseClass(new GroovyCodeSource(capellaScriptFile.getLocation().toFile(), capellaScriptFile.getCharset()));
InvokerHelper.runScript(cl, args);
import groovy.transform.*
@ToString
class Vehicle {
String brand
String model
Integer wheels
}
class Car extends Vehicle {}
@mariogarcia
mariogarcia / Hawk.groovy
Created May 26, 2015 09:40
POF about a rest-testing-autodoc-framework in Groovy/Java
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*
import org.codehaus.groovy.ast.stmt.*
String code = '''
class PersonSpec {
@mariogarcia
mariogarcia / withSession
Created March 3, 2015 11:16
withnewsession
UserDetails getUserDetails(String username) {
return UserDetails.findByUsername(username)
}
UserRole getUserRole(String username) {
return UserRole.findByUserName(username)
}
UserDetails getUserDetailsNoMatterWhat(String username) {
@mariogarcia
mariogarcia / Dockerfile
Last active August 29, 2015 14:10 — forked from melix/Dockerfile
################################################
# Dockerfile to run Groovy containers
# Based on Java 8 image
################################################
FROM java:8u40-jdk
MAINTAINER Cédric Champeau
RUN apt-get update
@mariogarcia
mariogarcia / TryMonadExample.groovy
Created October 18, 2014 23:36
How Try monad can be used in Groovy
import static java.lang.Integer.*
import static groovyfp.categories.Fn.*
def zero = { 0 }
def parse = { String c -> return { parseInt(c) } }
def addOne = { Integer y -> y + 1 }
def addToListIfValue = { Integer z -> z ? List(z) : List() }
// Getting every item in the list
@mariogarcia
mariogarcia / list_comprehensions_good_ideas.groovy
Created September 16, 2014 21:16
Ideas on how to implement some list comprehensions
import static groovyfp.categories.Fn.*
import groovyfp.categories.*
/*
a = do
x <- (1..3)
y <- ('a','b')
return (x,y)
a = mdo {
@mariogarcia
mariogarcia / list_comprehension.groovy
Last active August 29, 2015 14:06
silly list comprehension
def elements = [1..4, 30..50, 'a'..'d']
def iterators = elements*.iterator()
while(iterators*.hasNext().every()) {
println iterators*.next()
}
@mariogarcia
mariogarcia / bind.java
Created September 12, 2014 13:32
ListMonad bind draft implementation
@Override
public <B, M extends Monad<B>> M bind(Function<A, M> fn) {
List<B> result = new ArrayList<>();
for(A v : this.getTypedRef().getValue()) {
ListMonad<B> partialMonad = (ListMonad<B>) fn.apply(v);
result.addAll(partialMonad.getTypedRef().getValue());
}
return (M) result;
}
@mariogarcia
mariogarcia / BusinnessValidation.groovy
Created August 25, 2014 12:22
Thinking about business validation + functional style
interface HasUser {
User getUser()
}
interface HasBank {
Bank getBank()
}
interface Validator<T> {
Boolean validate(T validateable)