Skip to content

Instantly share code, notes, and snippets.

@erica
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/62d9f1ecee14f301dd1d to your computer and use it in GitHub Desktop.
Save erica/62d9f1ecee14f301dd1d to your computer and use it in GitHub Desktop.
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
Copy link

👍 very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment