...

Code Block |
---|

assert [1, 2, 3].find{ it > 1 } == 2 assert [1, 2, 3].findAll{ it > 1 } == [2, 3] assert ['a','b','c','d','e'].findIndexOf{ it in ['c','e','g'] } == 2 //find first item that satisfies closure assert [1, 2, 3].every{ it < 5 } assert ! [1, 2, 3].every{ it < 3 } assert [1, 2, 3].any{ it > 2 } assert ! [1, 2, 3].any{ it > 3 } // sum anything with a plus() method assert [1,2,3,4,5,6].sum() == 21 assert ['a','b','c','d','e'].sum{ it=='a'?1: it=='b'?2: it=='c'?3: it=='d'?4: it=='e'?5: 0 } == 15 assert ['a','b','c','d','e'].sum{ (char)it - (char)'a' } == 10 assert ['a','b','c','d','e'].sum() == 'abcde' assert [['a','b'], ['c','d']].sum() == ['a','b','c','d'] // an initial value can be provided assert [].sum(1000) == 1000 assert [1, 2, 3].sum(1000) == 1006 assert [1, 2, 3].join('-') == '1-2-3' assert [1, 2, 3].inject('counting: '){ str, item -> str + item } == 'counting: 123' assert [1, 2, 3].inject(0){ count, item -> count + item } == 6 |

...