[swift-users] Appending '[element]' recursively

Adriano Ferreira adriano.ferreira at me.com
Mon Jun 27 11:44:45 CDT 2016


Hi,

So, I tried :

// Swift 3

func quickSort(_ array: [Int]) -> [Int] {
    guard array.count > 1 else {
        return array
    }

    let (pivot, rest) = (array.first!, array.dropFirst())

    let lessThan = rest.filter({ $0 < pivot })
    let greaterThanOrEqual = rest.filter({ $0 >= pivot })

    // If a separate the sorting into variables rather than
    // calling them direct before 'return' it works
    let lessThanSorted = quickSort(lessThan)
    let greaterThanOrEqualSorted = quickSort(greaterThanOrEqual)

    return lessThanSorted + [pivot] + greaterThanOrEqualSorted
}

Here the exact error Iā€™m getting:



Thanks for helping.

ā€” A


> On Jun 27, 2016, at 12:38 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
> 
> On Mon, Jun 27, 2016 at 7:48 AM, Adriano Ferreira via swift-users
> <swift-users at swift.org> wrote:
>> // Swift 3
>> 
>> func quickSort(_ array: [Int]) -> [Int] {
>>    guard array.count > 1 else {
>>        return array
>>    }
>> 
>>    let (pivot, rest) = (array.first!, array.dropFirst())
>> 
>>    let lessThan = rest.filter({ $0 < pivot })
>>    let greaterThanOrEqual = rest.filter({ $0 >= pivot })
>> 
>>    // Cannot use ā€˜[Int]' here, as in Swift 2.2
>>    // Error says 'Int' is not convertible to '[Int]'
>>    let pivotArray = [pivot]
>>    return quickSort(lessThan) + pivotArray + quickSort(greaterThanOrEqual)
>> }
> 
> Hi Adriano,
> 
> This code compiles with the current git master.  Could you
> double-check and post the exact error you are getting?
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160627/68cd8cfe/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2016-06-27 at 12.40.42 PM.png
Type: image/png
Size: 110363 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160627/68cd8cfe/attachment.png>


More information about the swift-users mailing list