Skip to content

Instantly share code, notes, and snippets.

@timyates
Created February 16, 2012 15:43
Show Gist options
  • Select an option

  • Save timyates/1845755 to your computer and use it in GitHub Desktop.

Select an option

Save timyates/1845755 to your computer and use it in GitHub Desktop.
Working with Groovy snippets 2
(onearginject*) $ git add src/test/groovy/ClosureMethodTest.groovy
(onearginject*) $ git commit -m "Added tests for new one arg inject"
[onearginject fe2e59d] Added tests for new one arg inject
1 files changed, 17 insertions(+), 0 deletions(-)
(onearginject) $ ant test -DtestCase=groovy.ClosureMethodTest
...snip...
[junit] Testcase: testOneArgListInject(groovy.ClosureMethodTest): Caused an ERROR
[junit] No signature of method: java.util.ArrayList.inject() is applicable for argument types: (groovy.ClosureMethodTest$_testOneArgListInject_closure48) values: [groovy.ClosureMethodTest$_testOneArgListInject_closure48@4b8a6e6e]
[junit] Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), collect(), collect(), toSet()
[junit] groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.inject() is applicable for argument types: (groovy.ClosureMethodTest$_testOneArgListInject_closure48) values: [groovy.ClosureMethodTest$_testOneArgListInject_closure48@4b8a6e6e]
[junit] Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), collect(), collect(), toSet()
[junit] at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
[junit] at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
[junit] at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
[junit] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
[junit] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
[junit] at groovy.ClosureMethodTest.testOneArgListInject(ClosureMethodTest.groovy:237)
[junit]
[junit]
[junit] Test groovy.ClosureMethodTest FAILED
/**
* Performs the same function as the version of inject that takes an initial value, but
* uses the head of the Collection as the initial value, and iterates over the tail.
* <pre class="groovyTestCase">
* assert 1 * 2 * 3 * 4 == [ 1, 2, 3, 4 ].inject { acc, val -> acc * val }
* assert ['b'] == [['a','b'], ['b','c'], ['d','b']].inject { acc, val -> acc.intersect( val ) }
* LinkedHashSet set = [ 't', 'i', 'm' ]
* assert 'tim' == set.inject { a, b -> a + b }
* </pre>
*
* @param self a Collection
* @param initialValue some initial value
* @param closure a closure
* @return the result of the last closure call or null if called on an empty Collection
* @see #inject(Collection, Object, Closure)
*/
public static <T, V extends T> T inject(Collection<T> self, Closure<V> closure ) {
if( self.size() == 0 ) {
return null ;
}
List<T> list = asList( self ) ;
if( list.size() == 1 ) {
return list.get( 0 ) ;
}
return (T) inject( drop( list, 1 ), head( list ), closure ) ;
}
(onearginject*) $ ant test -DtestCase=groovy.ClosureMethodTest
...snip...
-testOne:
[junit] WARNING: multiple versions of ant detected in path for junit
[junit] jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit] and jar:file:/Users/tyates/Code/Groovy/GROOVYDEV/groovy-core/target/lib/compile/ant-1.8.2.jar!/org/apache/tools/ant/Project.class
[junit] Running groovy.ClosureMethodTest
[junit] Testsuite: groovy.ClosureMethodTest
[junit] Tests run: 24, Failures: 0, Errors: 0, Time elapsed: 0.413 sec
[junit] Tests run: 24, Failures: 0, Errors: 0, Time elapsed: 0.413 sec
...snip...
BUILD SUCCESSFUL
Total time: 19 seconds
(onearginject*) $ git commit -a -m "Added implementation for single arg inject"
[onearginject 8f705ec] Added implementation for single arg inject
1 files changed, 27 insertions(+), 0 deletions(-)
(onearginject) $ ant test
...snip...
BUILD SUCCESSFUL
Total time: 6 minutes 19 seconds
(onearginject) $ git push origin onearginject
Warning: No xauth data; using fake authentication data for X11 forwarding.
Counting objects: 68, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (39/39), done.
Writing objects: 100% (45/45), 4.69 KiB, done.
Total 45 (delta 33), reused 0 (delta 0)
To git@github.com:timyates/groovy-core.git
* [new branch] onearginject -> onearginject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment