Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-25 Thread Haravikk via swift-evolution
> On 25 May 2016, at 16:51, Jeremy Pereira > wrote: >> On 24 May 2016, at 12:33, Haravikk via swift-evolution >> wrote: >>> On 23 May 2016, at 20:19, Dany St-Amant via swift-evolution >>> wrote: >>> >>> Challenge accepted… Removed the if/else if, at the cost of double function >>> call to

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-25 Thread Jeremy Pereira via swift-evolution
> On 24 May 2016, at 12:33, Haravikk via swift-evolution > wrote: > > >> On 23 May 2016, at 20:19, Dany St-Amant via swift-evolution >> wrote: >> >> Challenge accepted… Removed the if/else if, at the cost of double function >> call to a tri-op: >> >> extension Bool { >> func as01() ->

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-25 Thread Charlie Monroe via swift-evolution
Ok, how about this? The compiler currently crashes when compiling this, but IMHO it is (or should be) valid code. enum IntegerSign: NumberType { case Negative = -1 case Zero = 0 case Positive = 1 var signum: NumberType { return self.rawValue } } extensi

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-24 Thread David Sweeris via swift-evolution
That depends... Does “x = a ? b : c” and "if a {x = b} else {x = c}” compile down to the same machine code? I know of one arch where “?:” takes 0 cycles, but I don’t know if there’s a difference on x86 or ARM. Either way, I’d think that just doing the comparison within `sign` would be faster, s

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-24 Thread David Sweeris via swift-evolution
The RawValue is an Int, but signum is a T, which I thought was the point. I tried saying `T: IntegerLiteralConvertible`, and making RawValue = T, but the playground didn’t like that. Maybe with some of Swift 3’s generic enhancements, it could be that simple. In any case, it sounds like this mig

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-24 Thread Haravikk via swift-evolution
> On 23 May 2016, at 20:19, Dany St-Amant via swift-evolution > wrote: > > Challenge accepted… Removed the if/else if, at the cost of double function > call to a tri-op: > > extension Bool { > func as01() -> T { return self ? 1 : 0 } > } > > extension SignedNumberType { > var sign: S

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Charlie Monroe via swift-evolution
Yes, but here, the rawValue is Int - in my code, the rawValue is NumberType, which IMHO makes more sense... > On May 24, 2016, at 7:36 AM, David Sweeris wrote: > > Sorry, I misspoke. I just meant define it like this: > public enum IntegerSign : Int { > case Negative = -1 > case Zero = 0

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread David Sweeris via swift-evolution
Sorry, I misspoke. I just meant define it like this: public enum IntegerSign : Int { case Negative = -1 case Zero = 0 case Positive = 1 public var signum: T { return T(self.rawValue.toIntMax()) } } Although, come to think of it, I’m not sure if that’s an exact drop-in

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Charlie Monroe via swift-evolution
Sure, that's a good idea, though I'd personally use the signum var, since using rawValue seems like a bit of an abuse of the fact the enum is defined this way and doesn't help readability of the code: enum IntegerSign: RawRepresentable { case Negative case Zero case Positive

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread David Sweeris via swift-evolution
Can we make it RawRepresentable? That way signum can just return self.rawValue Sent from my iPhone > On May 23, 2016, at 06:05, Charlie Monroe via swift-evolution > wrote: > > The clean way would be to make it an enum with var signum that would return > -1, 0, 1: > > enum IntegerSign { > >

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Dany St-Amant via swift-evolution
> Le 22 mai 2016 à 03:07, Adam Nemecek via swift-evolution > a écrit : > > Howdy, > I think that the SignedNumberType should implement a method called sign that > will return -1 for negative numbers, 0 for 0 and 1 for positive numbers. This > is similar to the signum method in e.g. Java and s

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Charlie Monroe via swift-evolution
The clean way would be to make it an enum with var signum that would return -1, 0, 1: enum IntegerSign { case Negative case Zero case Positive var signum: NumberType { switch self { case .Negative: return -1 as NumberType case .Zero:

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Adam Nemecek via swift-evolution
Oh and also machine learning. Search for sign or signum and machine learning, you'll find a lot of results. On Mon, May 23, 2016 at 1:26 AM, Adam Nemecek wrote: > It's not for branching based on the value, it's for calculating > mathematical functions with the sign retrieved from the value. So f

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Adam Nemecek via swift-evolution
It's not for branching based on the value, it's for calculating mathematical functions with the sign retrieved from the value. So for the same reason, no it should not be an enum. It should be the same type as the type it's called on. It's that way in Haskell as well http://hackage.haskell.org/pac

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-23 Thread Haravikk via swift-evolution
Could you give an example of this method’s usage? Surely your value is either positive, negative or zero already, so this method doesn’t return anything more useful. In other words, anywhere that I might do this: if myValue.sign > 0 { … } I could just as easily do: if myValue

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-22 Thread Adrian Zubarev via swift-evolution
Whoops didn’t check that your example worked as well :D --  Adrian Zubarev Sent with Airmail Am 22. Mai 2016 bei 10:27:17, Adrian Zubarev (adrian.zuba...@devandartist.com) schrieb: This should do the trick: extension SignedNumberType { var sign: Self { if self =

Re: [swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-22 Thread Adrian Zubarev via swift-evolution
This should do the trick: extension SignedNumberType { var sign: Self { if self == (0 as Self) { return (0 as Self) } else if self > (0 as Self) { return (1 as Self) } return (-1 as Self) } } --  Adrian Zubarev Sent

[swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.

2016-05-22 Thread Adam Nemecek via swift-evolution
Howdy, I think that the SignedNumberType should implement a method called sign that will return -1 for negative numbers, 0 for 0 and 1 for positive numbers. This is similar to the signum method in e.g. Java and similarly called methods in other languages. The implementation is fairly straight forw