[swift-evolution] Proposal: for loops with return values

Craig Cruden ccruden at novafore.com
Tue Jan 19 09:08:06 CST 2016


i.e. the Swift way of doing things that comes to mind (equivalent) would be:

let host: String? = “host”
let port: Int? = “80”

let inetAddress = InetSocketAddress?
if let host = host, port = port {
  inetAddress = InetSocketAddress(host, port)
}
else {
  inetAddress = nil
}

Just wondering if there was a cleaner way - because to me that just looks ugly.

(or the map/flatmap route which would probably not be immediately understandable to some)


> On 2016-01-19, at 21:20:21, Amir Michail <a.michail at me.com> wrote:
> 
> 
>> On Jan 19, 2016, at 7:26 AM, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>> 
>> 
>> What is the most readable/concise (balance) for writing something like the following in swift:
>> 
>> val host: Option[String] = Some("host")
>> val port: Option[Int] = Some(80)
>> val addr = for {
>>   h <- host
>>   p <- port
>> } yield new InetSocketAddress(h, p)
> 
> Wouldn’t “let/val { ...” be better than “for { ...” in this context?
> 
>> 
>> i.e. If either of the optionals is None then the result will be None otherwise it will be Some(inetSocketAddress).
>> 
>> 
>>> On 2016-01-15, at 5:57:33, Thorsten Seitz <tseitz42 at icloud.com <mailto:tseitz42 at icloud.com>> wrote:
>>> 
>>> Scala’s for-comprehensions are very powerful, precisely because they are using flatMap etc.
>>> This allows using them with other types instead of collections, e.g. asynchronous calls:
>>> 
>>> func someAsyncCall() -> Future<Int> { … }
>>> func someOtherAsyncCall() -> Future<Int> { … }
>>> 
>>> //  mixing Scala and Swift syntax here...
>>> let z: Future<Int> = for {
>>> 	x <- someAsyncCall()
>>> 	y <- someOtherAsyncCall()
>>> } yield (x + y)
>>> 
>>> The important thing for this to look nice is that there is no nesting like in your Swift example. Otherwise you get a pyramid of doom (think of more async calls).
>>> 
>>> Basically it is the same as Haskell’s do notation and you can do powerful things with that like the async example. Another nice example is the abstraction of Bayes’ rules presented in this blog: http://www.randomhacks.net/2007/02/22/bayes-rule-and-drug-tests/ <http://www.randomhacks.net/2007/02/22/bayes-rule-and-drug-tests/>
>>> 
>>> -Thorsten
>>> 
>>> 
>>>> Am 14.01.2016 um 22:10 schrieb Craig Cruden via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>>>> 
>>>> I believe this is the same as Scala’s for-comprehension….. and Scala’s for-comprehension is actually just syntactic sugar for a combination of flatMap, map and withFilter.
>>>> 
>>>> Although harder to read for many… it likely could be written similarly in Swift.  
>>>> 
>>>> i.e. 
>>>> 
>>>>>> for row in 0..<m { for col in 0..<n where row+col < 5 { yield (row,col) } }
>>>> 
>>>> in Scala is
>>>> 
>>>> for {
>>>>    row <- 0 to m 
>>>>    col <- 0 to n
>>>>    if row+col < 5
>>>> } yield (row, col)
>>>> 
>>>> which is actually (after the compiler is done with it):
>>>> 
>>>> (0 to m).flatMap(x => (0 to n).withFilter(y => x + y < 5).map(y => (x, y)))
>>>> 
>>>> I have not tried to do it in the playground but I would expect since I believe all the functions are available in Swift.
>>>> 
>>>> Note: 0 to m // is a range in Scala
>>>> 	withFilter is a filter which does not copy the contents of what it is filtering…. it is just applying the filter as needed when mapping.
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>>> On 2016-01-15, at 3:35:23, Félix Cloutier via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>> 
>>>>> This has been proposed before and Chris said that it would be out of scope for Swift 3. There is definitely interest in this area though.
>>>>> 
>>>>> Félix
>>>>> 
>>>>>> Le 14 janv. 2016 à 15:30:07, Amir Michail via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
>>>>>> 
>>>>>> These would be like the unrolled versions of Python’s generator expressions.
>>>>>> 
>>>>>> Examples:
>>>>>> 
>>>>>> let a:[Int] = for x in l { yield x*2 }
>>>>>> 
>>>>>> let b:[(Int,Int)] = for row in 0..<m { for col in 0..<n { yield (row,col) } }
>>>>>> 
>>>>>> let c:[(Int,Int)] = for row in 0..<m { for col in 0..<n where row+col < 5 { yield (row,col) } }
>>>>>> 
>>>>>> _______________________________________________
>>>>>> swift-evolution mailing list
>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>> 
>>>>> _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>> 
>>>>  _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
> 

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


More information about the swift-evolution mailing list