[swift-evolution] [Proposal] Add .order() family of methods to Collection

Taras Zakharko taras.zakharko at uzh.ch
Mon Apr 11 13:38:58 CDT 2016


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160411/2a3d8510/attachment.html>


More information about the swift-evolution mailing list