[swift-evolution] ternary operator ?: suggestion

Jo Albright me at jo2.co
Fri Jan 8 03:43:39 CST 2016


Makes sense. I like the idea of having a pattern matching case system for closures. Would be very useful across all sorts of functions, including custom closures developers create. 

 Nerd . Designer . Developer
Jo Albright


> On Jan 8, 2016, at 4:22 AM, Craig Cruden <ccruden at novafore.com> wrote:
> 
> Conceivably anywhere you accept a function - you could accept a set of functions based on pattern matching.
> 
> 
>> On 2016-01-08, at 15:53:09, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>> 
>> There is no reason why it could not be called that…. but…. it would be much more restrictive (then of course you would have switchFilter, switchReduce, switchFold etc.
>> 
>> The idea is that instead of having just a map which takes only a single function, in now takes a set of functions that are applicable based on pattern matching conditions (the entire set covers all cases).  
>> 
>> In the case of reduce, I could take a list of types which apply different calculations depending on type which sum together to produce a single value.  
>> 
>> Of you could just have a single case that covers all cases but allows you to decompose a type/value into something more friendly to work with.
>> 
>> i.e. key/value ….  n.map { case (k, v) in …. }
>> 
>> 
>>> On 2016-01-08, at 15:35:30, Jo Albright <me at jo2.co <mailto:me at jo2.co>> wrote:
>>> 
>>>> On Jan 8, 2016, at 2:36 AM, Craig Cruden via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>>>> 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
>>>>>> }
>>> 
>>> Is there any reason it couldn’t be “switchMap” instead of “map”? Would follow the design of “flatMap” naming. It would also help explain what is happening by mapping based on a switch statement.
>>> 
>>> let commission = trade.switchMap {
>>> 
>>>     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
>>> 
>>> }
>>> 
>>> 
>>>  Nerd . Designer . Developer
>>> Jo Albright
>>> 
>>> 
>> 
> 

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


More information about the swift-evolution mailing list