Re: [swift-users] Looping through all values between UInt8.min and UInt8.max

2016-04-08 Thread Nate Cook via swift-users
You can stride through the maximum value of a type if you land right on it:

for i in UInt8.min.stride(through: UInt8.max, by: 1) {
print(i)
}

Nate

> On Apr 8, 2016, at 9:21 AM, Erica Sadun  wrote:
> 
> 
>> On Apr 8, 2016, at 3:48 AM, tuuranton--- via swift-users 
>>  wrote:
>> 
>> print(UInt8.min) //0
>> 
>> print(UInt8.max) //255
>> 
>> 
>> 
>> //Is there an easy way to loop between all values
>> 
>> //between (and including both) UInt8.min and UInt8.max?
>> 
>> 
>> 
>> //This doesn't work.
>> 
>> //Runtime crash because UInt8.max has no successor.
>> 
>> for i in UInt8.min...UInt8.max {
>> 
>> print(i)
>> 
>> }
>> 
> 
> Easy? No. 
> 
> // Range Index has no valid successor
> for i in UInt8.min ... UInt8.max {
> print(i)
> }
> 
> // '...' cannot be applied to two 'UInt8' operands
> for i in UInt8.min ... UInt8.max as ClosedInterval {
> print(i)
> }
> 
> // Stops at 254
> for i in UInt8.min ..< UInt8.max {
> print(i)
> }
> 
> 
> This works, but ugh:
> 
> for i : Int in numericCast(UInt8.min)...numericCast(UInt8.max) {
> guard let j: UInt8 = UInt8(i) else { fatalError("Nope") }
> print(j)
> }
> 
> cc'ing in a few people to the white courtesy range phone.
> 
> -- E
> 
> 
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Looping through all values between UInt8.min and UInt8.max

2016-04-08 Thread Milos Rankovic via swift-users
Hi Erica, 

Wouldn’t this work for @tuuranton’s purposes?


let allUInt8s = UInt8.min.stride(through: UInt8.max, by: 1)

allUInt8s.dynamicType // StrideThrough.Type

Array(allUInt8s).count // 256

for i in allUInt8s {
i // 0...255
}

milos

> On 8 Apr 2016, at 13:48, tuuranton--- via swift-users  > wrote:
> 
> The problem with both solutions is that I lose the type information. Instead 
> of i being UInt8 it is Int or UInt.
> 
> This is one solution I came up with:
> 
> extension UInt8 {
> static var allValues: [UInt8] {
> var v: [UInt8] = []
> for i in 0.. v.append(i)
> }
> v.append(UInt8.max)
> return v
> }
> }
> 
> print(UInt8.allValues)
> 
> In other words, loop from 0 to 254 and then add 255 manually at the end.
> 
> Is there currently no simpler way to do this in Swift?
> 
> Idea for the Swift developers: Create a Bounded protocol that UInt8 (and the 
> other integer types) conform to. Then add a "allValues" type method in that 
> protocol. And also put .min and .max into that protocol. Currently it seems 
> like .min and .max are not a part of any particular protocol.
> 
> 
> 8. Apr 2016 14:35 by swift-users@swift.org :
> 
> You could cast the min and max values to an Int and iterate through the Int 
> range:
> 
> let min = Int(UInt8.min)
> let max = Int(UInt8.max)
> 
> for i in min...max {
> print(i)
> }
> 
> While this will work it seems like a hack to me and Milos’ solution is more 
> elegant…
> 
> -Pete
> 
> 
> On Apr 8, 2016, at 5:50 AM, tuuranton--- via swift-users 
> > wrote:
> 
> I forgot to mention that I really would like to have i have type UInt8 within 
> the loop. And since i is UInt8 it makes sense it should be able to take the 
> values UInt8.min and UInt8.max (and all the values between).
> 
> 8. Apr 2016 11:48 by swift-users@swift.org :
> 
> print(UInt8.min) //0
> 
> print(UInt8.max) //255
> 
> 
> 
> //Is there an easy way to loop between all values
> 
> //between (and including both) UInt8.min and UInt8.max?
> 
> 
> 
> //This doesn't work.
> 
> //Runtime crash because UInt8.max has no successor.
> 
> for i in UInt8.min...UInt8.max {
> 
> print(i)
> 
> }
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users

> On 8 Apr 2016, at 15:21, Erica Sadun via swift-users  
> wrote:
> 
>> 
>> On Apr 8, 2016, at 3:48 AM, tuuranton--- via swift-users 
>> > wrote:
>> 
>> print(UInt8.min) //0
>> 
>> print(UInt8.max) //255
>> 
>> 
>> 
>> //Is there an easy way to loop between all values
>> 
>> //between (and including both) UInt8.min and UInt8.max?
>> 
>> 
>> 
>> //This doesn't work.
>> 
>> //Runtime crash because UInt8.max has no successor.
>> 
>> for i in UInt8.min...UInt8.max {
>> 
>> print(i)
>> 
>> }
>> 
>> 
> 
> Easy? No. 
> 
> // Range Index has no valid successor
> for i in UInt8.min ... UInt8.max {
> print(i)
> }
> 
> // '...' cannot be applied to two 'UInt8' operands
> for i in UInt8.min ... UInt8.max as ClosedInterval {
> print(i)
> }
> 
> // Stops at 254
> for i in UInt8.min ..< UInt8.max {
> print(i)
> }
> 
> 
> This works, but ugh:
> 
> for i : Int in numericCast(UInt8.min)...numericCast(UInt8.max) {
> guard let j: UInt8 = UInt8(i) else { fatalError("Nope") }
> print(j)
> }
> 
> cc'ing in a few people to the white courtesy range phone.
> 
> -- E
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Looping through all values between UInt8.min and UInt8.max

2016-04-08 Thread Erica Sadun via swift-users

> On Apr 8, 2016, at 3:48 AM, tuuranton--- via swift-users 
>  wrote:
> 
> print(UInt8.min) //0
> 
> print(UInt8.max) //255
> 
> 
> 
> //Is there an easy way to loop between all values
> 
> //between (and including both) UInt8.min and UInt8.max?
> 
> 
> 
> //This doesn't work.
> 
> //Runtime crash because UInt8.max has no successor.
> 
> for i in UInt8.min...UInt8.max {
> 
> print(i)
> 
> }
> 
> 

Easy? No. 

// Range Index has no valid successor
for i in UInt8.min ... UInt8.max {
print(i)
}

// '...' cannot be applied to two 'UInt8' operands
for i in UInt8.min ... UInt8.max as ClosedInterval {
print(i)
}

// Stops at 254
for i in UInt8.min ..< UInt8.max {
print(i)
}


This works, but ugh:

for i : Int in numericCast(UInt8.min)...numericCast(UInt8.max) {
guard let j: UInt8 = UInt8(i) else { fatalError("Nope") }
print(j)
}

cc'ing in a few people to the white courtesy range phone.

-- E


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Looping through all values between UInt8.min and UInt8.max

2016-04-08 Thread Milos Rankovic via swift-users
In that case, at least make it lazy:

let allUInt8s = UInt8.min.stride(through: UInt8.max, by: 1)

allUInt8s.dynamicType // StrideThrough.Type

Array(allUInt8s).count // 256

for i in allUInt8s {
i // 0...255
}

milos

> On 8 Apr 2016, at 13:48, tuuranton--- via swift-users  
> wrote:
> 
> The problem with both solutions is that I lose the type information. Instead 
> of i being UInt8 it is Int or UInt.
> 
> This is one solution I came up with:
> 
> extension UInt8 {
> static var allValues: [UInt8] {
> var v: [UInt8] = []
> for i in 0.. v.append(i)
> }
> v.append(UInt8.max)
> return v
> }
> }
> 
> print(UInt8.allValues)
> 
> In other words, loop from 0 to 254 and then add 255 manually at the end.
> 
> Is there currently no simpler way to do this in Swift?
> 
> Idea for the Swift developers: Create a Bounded protocol that UInt8 (and the 
> other integer types) conform to. Then add a "allValues" type method in that 
> protocol. And also put .min and .max into that protocol. Currently it seems 
> like .min and .max are not a part of any particular protocol.
> 
> 
> 8. Apr 2016 14:35 by swift-users@swift.org :
> 
> You could cast the min and max values to an Int and iterate through the Int 
> range:
> 
> let min = Int(UInt8.min)
> let max = Int(UInt8.max)
> 
> for i in min...max {
> print(i)
> }
> 
> While this will work it seems like a hack to me and Milos’ solution is more 
> elegant…
> 
> -Pete
> 
> 
> On Apr 8, 2016, at 5:50 AM, tuuranton--- via swift-users 
> > wrote:
> 
> I forgot to mention that I really would like to have i have type UInt8 within 
> the loop. And since i is UInt8 it makes sense it should be able to take the 
> values UInt8.min and UInt8.max (and all the values between).
> 
> 8. Apr 2016 11:48 by swift-users@swift.org :
> 
> print(UInt8.min) //0
> 
> print(UInt8.max) //255
> 
> 
> 
> //Is there an easy way to loop between all values
> 
> //between (and including both) UInt8.min and UInt8.max?
> 
> 
> 
> //This doesn't work.
> 
> //Runtime crash because UInt8.max has no successor.
> 
> for i in UInt8.min...UInt8.max {
> 
> print(i)
> 
> }
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Looping through all values between UInt8.min and UInt8.max

2016-04-08 Thread tuuranton--- via swift-users
I forgot to mention that I really would like to have i have type UInt8 within 
the loop. And since i is UInt8 it makes sense it should be able to take the 
values UInt8.min and UInt8.max (and all the values between).

8. Apr 2016 11:48 by swift-users@swift.org:


>
> print(UInt8.min) //0
>
> print(UInt8.max) //255
>
>
>
>
> //Is there an easy way to loop between all values
>
> //between (and including both) UInt8.min and UInt8.max?
>
>
>
>
> //This doesn't work.
>
> //Runtime crash because UInt8.max has no successor.
>
> for i in UInt8.min...UInt8.max {
>
>     print(i)
>
> }
>
> ___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users