Hi, 

I think it depends on how expensive the indexed access is on the collection. I 
use it primarily on arrays, where self[i] essentially boils down to a pointer 
dereference, so I expect the generated code to be very efficient. Your version 
might be faster for collection with expensive element access, but it should be 
slower for arrays and the like, as it involves additional intermediate 
structure allocations and copies. 

— Taras


> Just a side note that you could also:
> 
> extension SequenceType {
>       func order(@noescape isOrderedBefore: (Generator.Element, 
> Generator.Element) -> Bool) -> [Int] {
>               return enumerate().sort{ isOrderedBefore($0.1, $1.1) }.map{ 
> $0.0 }
>       }
> }
> 
> (0...3).reverse().order(<) // [3, 2, 1, 0]
> 
> This way you can `order` all sequences, and it is more efficient as you don’t 
> fetch elements by index inside the `isOrderedBefore`. (You could also *not* 
> `map` at the end and return all the elements along with their original 
> indexes.)
> 
> milos

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to