Skip to content

Instantly share code, notes, and snippets.

@timyates
Created June 28, 2012 11:36
Show Gist options
  • Select an option

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

Select an option

Save timyates/3010812 to your computer and use it in GitHub Desktop.
inject with default initialValue
def nums = [ 10, 20, 30 ]
// First element of nums (10) is used as default value
def product = nums.inject { acc, num -> acc * num }
assert product == 6000
def nums = [ 10, 20, 30 ]
def product = nums.inject( 1 ) { acc, num -> acc * num }
assert product == 6000
def lists = [ [1, 2, 3], [2, 3, 4], [4, 5, 2] ]
lists.inject { a, b -> a.intersect( b ) }
def lists = [ [1, 2, 3], [2, 3, 4], [4, 5, 2] ]
lists.drop( 1 ).inject( lists.head() ) { a, b -> a.intersect( b ) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment