5 ms·
Quicksort in Swift
- kiwiwearables 12y agowas waiting for someone to put this out :) awesome.
- hadoukenio 12y agoUntil Apple open Swift, I'm sorry, it's DOA.
- melling 12y agoI'm sorry but that's silly. While I really hope that Swift does become a cross-platform language, it's pretty safe to assume that it will become the defacto Apple development language. It offers way too much in terms of developer productivity.
- kennywinker 12y agoDOA (Dead On Arrival) implies that Swift will not be adopted by developers. If you're saying that, I disagree. It will almost definitely become the default way to write iOS apps very quickly. If you're saying it's "dead to me", that's valid. It would be good to see them open up the Swift toolchain, and there will definitely be a significant percentage of devs who won't touch it unless they do.
- jaegerpicker 12y agoThis is WAY TOO early. Swift at the very least will very likely replace Objective-C which is nowhere near DOA. The fact is that there ARE a ton of developers that don't care if it's open or not.
- ketralnis 12y agoObjective-C is open source, but without Foundation et al it's pretty useless. As tied as Swift is to Cocoa, it's almost certainly going to be in the same boat. Put another way, how well does Objective-C do outside of Apple development?
- nextstep 12y agoI like the syntax of Swift
- Alupis 12y agoLooks like a bizarre mix of JavaScript and C to me. But then, hey... Objective-C syntax just offends me right-out ;-P
- imkevinxu 12y agoI can't wait until GitHub or someone adds in Swift syntax highlighting
- deleted 12y ago[deleted]
- alayne 12y agoWhat kind of monster writes i += 1?
- psuter 12y agoAmong others, people who have learned to take JSLint a little too seriously [1]. [1] http://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript http://stackoverflow.com/questions/971312/why-avoid-incremen...
- agildehaus 12y agoPython devs :)
- Igglyboo 12y agoAnyone who writes python
- neurobro 12y agoI do, only because I've never seen a convincing case where unary increment/decrement improves the code in any way (and I've searched).
- nkeets 12y agoSo, what convincing cases have you find of i += 1 over i = i + 1?
- neurobro 12y agoIt reduces code redundancy, especially when 'i' is something more descriptive. Though if someone were to claim that the redundancy of i=i+1 is a trivial price for settling on one consistent multipurpose construct, I wouldn't put up a fight. :)
- exogen 12y agoConsider a more descriptive variable name and hopefully it becomes obvious: sheepCounted += 1 vs. sheepCounted = sheepCounted + 1
- seanewest 12y agoI put your code on npm: https://www.npmjs.org/package/quicksort.swift https://www.npmjs.org/package/quicksort.swift Edit: ...and then swiftly unpublished. It is unclear to me the legal issues involved in forking and publishing unlicensed gists. http://stackoverflow.com/questions/4007674/whats-the-default-license-of-code-published-at-github http://stackoverflow.com/questions/4007674/whats-the-default...
- arrakeen 12y agowhy would you publish that on npm? i'm not familiar with the ecosystem, is there a precedent for publishing non-js source files on npm?
- awda 12y agoAnd why would you claim MIT license on the code which you didn't write and has no license listed?
- seanewest 12y agoI think it is good practice to license packages on npm, even when you are not the original author (which is very common on npm).
- Alupis 12y agoWell... technically something with no license is the most restrictive form of a license; as-in nobody has the right to use it nor republish it nor modify it. Not being the copyright holder forbids you from assigning a license. However, for a trivial piece of code as this... it could be argued it's not copyright-able and therefore you are OK... but just be careful in the future.
- bradleyjg 12y agoIf it's not copyrightable it's deceptive to add a purported copyright license. If it is, he has no right to relicense it. Either way not a good move.
- gilgoomesh 12y agoIf anyone's wondering, yes, there's a quickSort function already in the Swift standard library (no import required): var ar = [3456,45,53567,356,7225,47858] quickSort(&ar, Range(start: ar.startIndex, end: ar.endIndex)) // [45, 356, 3456, 7225, 47858, 53567] There's also a version that let's you specify the "less" comparator.
- jkubicek 12y agoHow did you discover this?
- gilgoomesh 12y agoUse a Swift standard library function in the documentation (like println). Command-Click on it to go to it's definition. You're taken to a header-like description of the standard library (assuming the index has been built for your project – sometimes you have to wait a few minutes in a new project).
- Alupis 12y agoWow... seems like a nightmare for several reasons: >Naming Constants and Variables >You can use almost any character you like for constant and >variable names, including Unicode characters: >let π = 3.14159 >let 你好 = "你好世界" >let 🐶🐮 = "dogcow" https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_399 https://developer.apple.com/library/prerelease/ios/documenta...
- tailrecursion 12y agoI think if you're going to make a strong criticism of a complete design and implementation that includes decisions that were almost certainly made deliberately, then you owe an explanation or some kind of argument. My position is that it's the language's job to allow expression of ideas and not its job to police or disallow bad sentences. I think such a thing [EDIT: disallowing bad sentences] is impossible and leads to COBOL-like languages. (Compilers can have options for coding standards, like "house rules" but I don't think it's the language's role.)
- Alupis 12y agoWell, besides adding to complexity of debugging or just reading/understanding of the codebase when the dev may not be familiar with say.. .Chinese characters, it may also limit which platforms it can run on. But, this is an apple language targeting only apple devices so the last part isn't much of an issue. Basically using unicode characters is comparable to purposeful obfuscation, are difficult to type (no keyboard i'm aware of has a "cow" key), difficult to read/remember, and may become wrongly converted if the code is copy/pasted into a text editor not supporting that character. Now, I don't think this constituted as a "strong criticism" nor warranted the downvote nor the expletive.
- ryanwhitney 12y agoI don't think that is what "several" means.
- Alupis 12y ago
- shawkinaw 12y agoThis actually highlights one area where I find Swift slightly deficient. I was just thinking earlier today that Swift is remarkably close to my dream language, i.e. incorporates the great features of many languages I've come in contact with. But one thing it lacks is Haskell's amazing list comprehension, which helps make Haskell quicksort so succinct.
- alayne 12y agoThat quicksort one-liner that you often see for Haskell is crap.
- dstaley 12y agoI was surprised this example didn't take advantage of Swift's built-in Comparable protocol, so I rewrote it! I also wrote an example of how you can implement the Comparable protocol for a struct, so that they can then be sorted. https://gist.github.com/dstaley/e7171375d3f5cb9f2fe4 https://gist.github.com/dstaley/e7171375d3f5cb9f2fe4
- brudgers 12y agoHoare's original paper from 1962. http://comjnl.oxfordjournals.org/content/5/1/10.full.pdf+html http://comjnl.oxfordjournals.org/content/5/1/10.full.pdf+htm... There are reasons not to perform quickSort the way people did fifty years ago. Data sets are larger and worst case time for quickSort approach 0(n^2) when data is already ordered and pivots are selected in-order. Randomizing pivot selection mitigates this back toward O(log n). All that mutation and pointer swapping are artifacts of the way things used to have to be done by programmers. And they're useful as academic exercises. Hoare's algorithm is really fucking brilliant. But mapping and filtering in a functional style, makes more sense than dotting the 'i's and 'j's if someone is going to be reading and maintaining the code. There's a reason people don't use C to program mobile devices. Moreover, while Hoare's original algorithm was designed to work in place, the idea of working in place is less coherent today. When we strive to distribute and parallelize big nasty resource intensive jobs, what does "in-place" mean? See Leslie Lamport, Thinking for Programmers at about time = 36:00 http://channel9.msdn.com/Events/Build/2014/3-642 http://channel9.msdn.com/Events/Build/2014/3-642 And even on a non-distributed system, when are we really coming up against the bounds of 'fast memory' these days? A serious problem that stretches 16 GigaBytes of RAM is less than $10,000 from 64 GigaBytes...or $200 from an SSD and using 'slow memory'. Of course if we are parallelizing, we can even do it on one machine and again, what is 'in-place'? The caches? Again which ones and at what level. The Swift code (or Java or C#) may look like C, but it's not. It may be garbage collected, JIT'd, byte coded, and micro-coded all before it hits the mulit-stage branch predicting pipeline from several levels of cache reading from code pages that may have gone in and out to disk as threads and tasks switch. 'Place' might just mean somewhere. But probably somewheres.
- djb_hackernews 12y agoHad a chat about Swift over lunch today, one topic was the choice of using .. and ... to represent ranges. Interesting to see the comments of the gist focus on this as well. Surely is a confusing and potentially dangerous syntax.
- gilgoomesh 12y agoInterestingly, the .. and ... operators are not part of the language but are defined in the standard library. This means you can override .. (to disable it) and define your own operator for half-closed ranges if you'd prefer (Swift supports building your own operators from combinations of punctuation characters). At least I think you could do this: I haven't tried.
- Jtsummers 12y agoInteresting, would it be possible, then, to use the perl6 style operators? (^ at either end to make it exclusive instead of inclusive)
- Someone 12y agoIf so, that opens entire new routes for obfuscation. For the subject at hand: I would like to have seen ranges use () for open endings and [] for closed ones. For example, [1..5) would include 1 but exclude (that is standard notation in mathematics. See http://en.wikipedia.org/wiki/Interval_(mathematics)#Excluding_the_endpoints http://en.wikipedia.org/wiki/Interval_(mathematics)#Excludin...) Back to the obfuscation thing: unfortunately for that goal, one cannot create prefix operators with names (, [ or { It is possible to define an infix operator named ., though. That could give whole new meaning to what look like floating point values such as 3.14.
- supersillyus 12y agoFrom that, it's not too hard how people could think it was influenced by Go (though it apparently wasn't): http://play.golang.org/p/oWNRd0D2Zp http://play.golang.org/p/oWNRd0D2Zp looks superficially very similar