[swift-evolution] ternary operator ?: suggestion

Craig Cruden ccruden at novafore.com
Fri Jan 8 01:36:04 CST 2016


> This provides a way to do a quick mapping that may not have been originally designed into the data structure which could be useful, and seems pretty powerful. 
> 
> What you are saying, I think, correct me if I am wrong, is that “map" could be applied on both containers and non-containers and “map” would be extended to support the switch like expression. I think exploring container types, even objects and structs, as the control value would be useful. This may lead to something useful,  and make sure that what is being proposed can support it in the future if not in the first release. 
> 
> - Paul
> 

Essentially yes…. it dawned on me that essentially what we are doing was pattern matching and then transforming (mapping) values using a function depending on which pattern was matched — and there was already something to do this called map.  It just needs to allow for pattern matching similar to switch.  The only thing was that we would need to be able to map (which would include pattern matching) on any value, tuple etc. as well as on collection containers.   It would be more powerful than just map, far more powerful than just a “switch expression” and cover a number of holes at the same time.  No changes to ternary would be needed, and no new matching expressions would have to be created.

To do this map would have to be implemented on things like Int, Float, tuples — in addition to containers and optionals.  

The pattern matching should be allowed on other functions like map, filter, reduce (left or right), fold (left or right) etc.

-Craig

>> On Jan 7, 2016, at 9:14 PM, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>> 
>> (forgot the other colons which would also become “in” not “:”
>> 
>> let commission = trade.map {
>> 	case .Buy(let quantity, let price) where Double(quantity) * price > 10000 in
>>     		Double(quantity) * price * vipCommissionRate / 100
>> 	case .Buy(let quantity, let price) in
>>     		Double(quantity) * price * commissionRate / 100
>> 	case .Sell(let quantity, let price) where Double(quantity) * price > 10000 in
>>     		Double(quantity) * price * vipCommissionRate / 100
>> 	case .Sell(let quantity, let price) in
>>     		Double(quantity) * price * commissionRate / 100
>> }
>> 
>> 
>> 
>>> On 2016-01-08, at 12:11:43, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>>> 
>>> I was thinking about this a bit, and was thinking that maybe we would not need a new keyword at all but just use `map` with pattern matching `case` clauses.  It would mean adding `map` to individual values — or anything that we might want to use as input for matching.  
>>> 
>>> Reference scala (section 8.5) pattern matching: http://www.scala-lang.org/docu/files/ScalaReference.pdf <http://www.scala-lang.org/docu/files/ScalaReference.pdf>
>>> 
>>> In scala inside `map` and `filter` (unfortunately not on things like `reduceLeft` or `foldLeft` you can specify pattern matching anonymous functions as such:
>>> 
>>> val num = List(1, 5, 7)
>>> 
>>> num.map {
>>>   case x if x < 5 => x + 1
>>>   case x => x - 1
>>> }
>>> 
>>> output: List(2, 4, 6)
>>> 
>>> So if the pattern matching was added (I don’t believe Swift currently allows case classes in maps - but then I am rather junior in the language) Swift would not need a `match expression` it would simply be part of map and become something like:
>>> 
>>> let commission = trade.map {
>>> 	case .Buy(let quantity, let price) where Double(quantity) * price > 10000 in
>>>     		Double(quantity) * price * vipCommissionRate / 100
>>> 	case .Buy(let quantity, let price):
>>>     		Double(quantity) * price * commissionRate / 100
>>> 	case .Sell(let quantity, let price) where Double(quantity) * price > 10000:
>>>     		Double(quantity) * price * vipCommissionRate / 100
>>> 	case .Sell(let quantity, let price):
>>>     		Double(quantity) * price * commissionRate / 100
>>> }
>>> 
>>> 
>>> 
>>> 
>>>> 
>>>> But if we go with brackets then I would recommend something like this: 
>>>> 
>>>> let commission = match (trade) {
>>>> case .Buy(let quantity, let price) where Double(quantity) * price > 10000:
>>>>     Double(quantity) * price * vipCommissionRate / 100
>>>> case .Buy(let quantity, let price):
>>>>     Double(quantity) * price * commissionRate / 100
>>>> case .Sell(let quantity, let price) where Double(quantity) * price > 10000:
>>>>     Double(quantity) * price * vipCommissionRate / 100
>>>> case .Sell(let quantity, let price):
>>>>     Double(quantity) * price * commissionRate / 100
>>>> }
>>> 
>> 
> 

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


More information about the swift-evolution mailing list