[swift-evolution] ternary operator ?: suggestion
Paul Ossenbruggen
possen at gmail.com
Fri Jan 8 01:00:50 CST 2016
This is interesting, I was kind of thinking along these lines too, that map and switch expressions had some similarity. In my proposal, as an aside, I briefly mention it as a possibility, I was thinking you could make the control value an array or dictionary and each element could be transformed using cases. My thoughts were the other way from what you suggest though, where you could map a container of values repetitively apply directly on the switch expression, without the word map. What you are suggesting, and apparently Scala, has is a way to use map with cases in it, presumably that works on arrays and dictionaries too.
From my proposal: if the ? operator could be used on containers as the control value, it would repetitively apply the expression on each element in the dictionary or an array.
[.North: "North", .South: "South", .East: "East", .West: "West", .Stay: "Stay"] ?(
case .North: ( 0, -1)
case .South: ( 0, 1)
case .East: (-1, 0)
case .West: ( 1, 0)
case _: ( 0, 0)
)
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
> On Jan 7, 2016, at 9:14 PM, Craig Cruden <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/20160107/989739b1/attachment.html>
More information about the swift-evolution
mailing list