Created
June 28, 2012 11:36
-
-
Save timyates/3010812 to your computer and use it in GitHub Desktop.
inject with default initialValue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nums = [ 10, 20, 30 ] | |
| def product = nums.inject( 1 ) { acc, num -> acc * num } | |
| assert product == 6000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def lists = [ [1, 2, 3], [2, 3, 4], [4, 5, 2] ] | |
| lists.inject { a, b -> a.intersect( b ) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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