Wednesday, February 12, 2014

detectIndex: Method

The #detectIndex: method is inconspicuous but something to remember when working with collections:

#( 11 7 4 21 ) detectIndex: #even  


#( 11 7 4 21 ) detectIndex: [:el | el < 5]

There is also a #detectIndex:ifNone: variant:

#( 1 3 5 7 ) detectIndex: #even ifNone: [ self error: 'no even number found' ]

Nice!

No comments: