@Override public Writable make(final Map map) { //we don't need a template.clone here as curry calls clone under the hood final Closure template = this.template.curry(new Object[]{map}); return (Writable) template; }
/** * Support for Closure currying. * * @param argument the argument to bind * @return the new closure with the argument bound * @see #curry(Object...) */ public Closure<V> curry(final Object argument) { return curry(new Object[]{argument}); }
private static Map fettleMethodMap(final Closure defaultGenerator, final Map methodMap) { final Map newMethodMap = new HashMap(); for (Object o : methodMap.keySet()) { final Object key = o; final Object value = methodMap.get(key); if ((value instanceof Closure)) { newMethodMap.put(key, value); } else { newMethodMap.put(key, defaultGenerator.curry((Object[]) value)); } } return newMethodMap; }
/** * Builds a trampolined variant of the current closure. * @param args Parameters to curry to the underlying closure. * @return An instance of TrampolineClosure wrapping the original closure after currying. */ @Override public Closure<V> trampoline(final Object... args) { return new TrampolineClosure<V>(original.curry(args)); }
/** * Builds a trampolined variant of the current closure. * To prevent stack overflow due to deep recursion, functions can instead leverage the trampoline mechanism * and avoid recursive calls altogether. Under trampoline, the function is supposed to perform one step of * the calculation and, instead of a recursive call to itself or another function, it return back a new closure, * which will be executed by the trampoline as the next step. * Once a non-closure value is returned, the trampoline stops and returns the value as the final result. * Here is an example: * <pre> * def fact * fact = { n, total -> * n == 0 ? total : fact.trampoline(n - 1, n * total) * }.trampoline() * def factorial = { n -> fact(n, 1G)} * println factorial(20) // => 2432902008176640000 * </pre> * * @param args Parameters to the closure, so as the trampoline mechanism can call it * @return A closure, which will execute the original closure on a trampoline. */ public Closure<V> trampoline(final Object... args) { return new TrampolineClosure<V>(this.curry(args)); }
public static void curryDelegateAndGetContent(Writer w, Closure c, Object o, boolean first) { StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first); Closure curried = c.curry(o); curried.setDelegate(delegate); curried.setResolveStrategy(Closure.DELEGATE_FIRST); curried.call(); } }
private Closure createTemplateClosure(List<StringSection> sections, final ClassLoader parentLoader, StringBuilder target) throws ClassNotFoundException { final GroovyClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() { public GroovyClassLoader run() { return new GroovyClassLoader(parentLoader); } }); final Class groovyClass; try { groovyClass = loader.parseClass(new GroovyCodeSource(target.toString(), TEMPLATE_SCRIPT_PREFIX + counter++ + ".groovy", "x")); } catch (MultipleCompilationErrorsException e) { throw mangleMultipleCompilationErrorsException(e, sections); } catch (Exception e) { throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage()); } Closure result; try { final GroovyObject object = (GroovyObject) groovyClass.newInstance(); Closure chicken = (Closure) object.invokeMethod("getTemplate", null); //bind the two first parameters of the generated closure to this class and the sections list result = chicken.curry(this, sections); } catch (InstantiationException e) { throw new ClassNotFoundException(e.getMessage()); } catch (IllegalAccessException e) { throw new ClassNotFoundException(e.getMessage()); } return result; }
/** * Factory method for creating <code>JsonDelegate</code>s from closures currying an object * argument. * * @param c closure representing JSON objects * @param o an object curried to the closure * @return an instance of <code>JsonDelegate</code> */ public static Map<String, Object> curryDelegateAndGetContent(Closure<?> c, Object o) { JsonDelegate delegate = new JsonDelegate(); Closure<?> curried = c.curry(o); curried.setDelegate(delegate); curried.setResolveStrategy(Closure.DELEGATE_FIRST); curried.call(); return delegate.getContent(); }
public void testListArithmetic() { List<List> numLists = new ArrayList<List>(); numLists.add(Arrays.asList(1, 2, 3)); numLists.add(Arrays.asList(10, 20, 30)); assertEquals(Arrays.asList(6, 60), collect(numLists, new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a + b + c; } })); Closure<Integer> arithmeticClosure = new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a * b + c; } }; Closure<Integer> tensAndUnits = arithmeticClosure.curry(10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(0, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(1, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); Closure<Integer> timesPlus5 = arithmeticClosure.rcurry(5); assertEquals(35, (int) timesPlus5.call(15, 2)); timesPlus5 = arithmeticClosure.ncurry(2, 5); assertEquals(35, (int) timesPlus5.call(15, 2)); }
private static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o, boolean first, JsonGenerator generator) { StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first, generator); Closure curried = c.curry(o); curried.setDelegate(delegate); curried.setResolveStrategy(Closure.DELEGATE_FIRST); curried.call(); }
/** * Factory method for creating <code>JsonDelegate</code>s from closures currying an object * argument. * * @param c closure representing JSON objects * @param o an object curried to the closure * @return an instance of <code>JsonDelegate</code> */ public static Map<String, Object> curryDelegateAndGetContent(Closure<?> c, Object o) { JsonDelegate delegate = new JsonDelegate(); Closure<?> curried = c.curry(o); curried.setDelegate(delegate); curried.setResolveStrategy(Closure.DELEGATE_FIRST); curried.call(); return delegate.getContent(); }
@Override public Writable make(final Map map) { //we don't need a template.clone here as curry calls clone under the hood final Closure template = this.template.curry(new Object[]{map}); return (Writable) template; }
/** * Support for Closure currying. * * @param argument the argument to bind * @return the new closure with the argument bound * @see #curry(Object...) */ public Closure<V> curry(final Object argument) { return curry(new Object[]{argument}); }
private static Map fettleMethodMap(final Closure defaultGenerator, final Map methodMap) { final Map newMethodMap = new HashMap(); for (Object o : methodMap.keySet()) { final Object key = o; final Object value = methodMap.get(key); if ((value instanceof Closure)) { newMethodMap.put(key, value); } else { newMethodMap.put(key, defaultGenerator.curry((Object[]) value)); } } return newMethodMap; }
private static Map fettleMethodMap(final Closure defaultGenerator, final Map methodMap) { final Map newMethodMap = new HashMap(); final Iterator keyIterator = methodMap.keySet().iterator(); while (keyIterator.hasNext()) { final Object key = keyIterator.next(); final Object value = methodMap.get(key); if ((value instanceof Closure)) { newMethodMap.put(key, value); } else { newMethodMap.put(key, defaultGenerator.curry((Object[])value)); } } return newMethodMap; }
/** * Builds a trampolined variant of the current closure. * @param args Parameters to curry to the underlying closure. * @return An instance of TrampolineClosure wrapping the original closure after currying. */ @Override public Closure<V> trampoline(final Object... args) { return new TrampolineClosure<V>(original.curry(args)); }
@Override public Closure curry(Object[] objects) { return createWrapper(this.target.curry(objects)); }
public List<Group> doCall(final List<Group> acc, final Group group) { final Closure<Statement> trx = mappings.get(group.label.name); final List<Statement> source = group.statements; final List<Statement> destin = trx == null ? collect(source) : collect(source, trx.curry(group)); acc.add(group.copyWithStatements(destin)); return acc; } };
private static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o, boolean first, JsonGenerator generator) { StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first, generator); Closure curried = c.curry(o); curried.setDelegate(delegate); curried.setResolveStrategy(Closure.DELEGATE_FIRST); curried.call(); }
public void callInit(ServletContext servletContext) { Closure<?> init = getInitClosure(); if (init != null) { Class[] parameterTypes = init.getParameterTypes(); if(parameterTypes != null) { init = init.curry(new Object[]{servletContext}); } Environment.executeForCurrentEnvironment(init); } }