Skip to content

Instantly share code, notes, and snippets.

@timyates
Created February 16, 2012 14:39
Show Gist options
  • Save timyates/1845334 to your computer and use it in GitHub Desktop.
Save timyates/1845334 to your computer and use it in GitHub Desktop.
Working with groovy snippets
$ git clone [email protected]:timyates/groovy-core.git
Cloning into groovy-core...
Warning: No xauth data; using fake authentication data for X11 forwarding.
remote: Counting objects: 144322, done.
remote: Compressing objects: 100% (32204/32204), done.
remote: Total 144322 (delta 92128), reused 144110 (delta 91947)
Receiving objects: 100% (144322/144322), 106.70 MiB | 5.17 MiB/s, done.
Resolving deltas: 100% (92128/92128), done.
$ cd groovy-core/
(master) $ git remote add upstream https://github.com/groovy/groovy-core.git
(master) $ git remote -v
origin [email protected]:timyates/groovy-core.git (fetch)
origin [email protected]:timyates/groovy-core.git (push)
upstream https://github.com/groovy/groovy-core.git (fetch)
upstream https://github.com/groovy/groovy-core.git (push)
(master) $ git pull upstream master
From https://github.com/groovy/groovy-core
* branch master -> FETCH_HEAD
Already up-to-date.
def result = list.drop( 1 ).inject( list.head() ) { a, b -> b << a }
def people = [ [ 'tim', 'dave', 'chris' ],
[ 'stuart', 'harry', 'tim' ],
[ 'bert', 'tim', 'ernie' ] ]
def inAll = people.drop( 1 ).inject( people.head() ) { a, b ->
a.intersect( b )
}
def result = list.inject { a, b -> b << a }
def inAll = people.inject { a, b -> a.intersect( b ) }
(master) $ git checkout -b onearginject
Switched to a new branch 'onearginject'
(onearginject) $
void testOneArgListInject() {
// Check basic functionality
def value = [ 1, 2, 3 ].inject { c, item -> c + item }
assert value == 6
// Check a use-case
value = [ [ 'tim', 'dave', 'chris' ],
[ 'stuart', 'harry', 'tim' ],
[ 'bert', 'tim', 'ernie' ] ]
assert value.inject { a, b -> a.intersect( b ) } == ['tim']
// Check edges
assert [].inject { a, b -> a + b } == null
assert [ 1 ].inject { a, b -> a + b } == 1
assert [ 1, 2 ].inject { a, b -> a + b } == 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment