Skip to content

Instantly share code, notes, and snippets.

@erica

erica/maxIndex.swift

Last active Aug 29, 2015
Embed
What would you like to do?
let array = [2, 3, 5, 1, 6, 9, 8]
extension Array where Element:Comparable {
var maxIndex : Int? {
// Thanks Mike Ash, Jacob Bandes-Storch, Bas Broek
return self.enumerate().maxElement({$1.element > $0.element})?.index
}
}
array.maxIndex
// Update: Thanks, Jordan Rose
// http://twitter.com/UINT_MIN/status/624274556098842624
extension CollectionType where Generator.Element: Comparable {
var maxIndex : Index? {return self.indices.maxElement({self[$1] > self[$0]})}
}
@Gerst20051

This comment has been minimized.

Copy link

@Gerst20051 Gerst20051 commented Aug 19, 2015

👍 very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.