Skip to content

Instantly share code, notes, and snippets.

def factorial ={ n ->
if (n == 0) 1
else n * factorial(n - 1)
}
factorial(4)
def factorial ={ n, accumulator=1 ->
if (n == 1) accumulator
else factorial(n-1, n*accumulator)
}
factorial(4)
def factorial ={ n, accumulator=1 ->
if (n == 1) accumulator
else factorial.trampoline(n-1, n*accumulator)
}.trampoline()
factorial(4)
def percentage = { percentage, x ->
x/100 * percentage
}
def tenPercent = percentage.curry(10)
def list = ['groovy', 'functional']
//This mutates the original list
list.add('programming')
//This creates a new list leaving the original unchanged
def newList = list.plus(2, 'programming')
class UnwelcomeVisitorIterator {
static public traverse( data, visitor ){
if ( data instanceof Map ){
data.each{ entrySet ->
entrySet.value = visitor.visitMap( entrySet.key, entrySet.value )
entrySet.value = traverse( entrySet.value, visitor )
}
} else if ( data instanceof Collection ){
def processedList = []
//This code iterates a nested Map/List structure and converts any Date objects it finds into
// String equivalents.
//def data = [:] - data is some complex nested Map/List data structure
UnwelcomeVisitor.traverse( data, [
visitMap: { key, value ->
if ( value instanceof Date ) value = value.toString()
value
},
visitList: { entry ->
if ( entry instanceof Date ) entry = entry.toString()
Entity people = schema.addEntity("People");
people.addIdProperty();
people.addStringProperty("name").notNull();
people.addStringProperty("emailAddress").notNull();
/*
* The entity for an email
*/
Entity email = schema.addEntity("Email");
email.addIdProperty().getProperty();
<plugin>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-maven-plugin</artifactId>
<version>1.4.5</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>