[swift-evolution] SE-0105: Removing Where Clauses from For-In Loops

Sean Heber sean at fifthace.com
Fri Jun 24 10:15:32 CDT 2016


I’ve occasionally thought something like that would be handy, but usually I find a way to reorganize it so it’s not necessary and it works just as well. Hard to say, really. Adding power might be good, but adding too much power might easily cross my own threshold of “too complex”.

I do think that proper consideration should be given to moving “where”. For example, here are a few of mine re-ordered:


for location where location.z == Global.cameraZ in path {
}

for below where contains(below) && hasSurfaceAt(below) in location.below.cardinals {
}

for neighbor where contains(neighbor) && isRampAt(neighbor) && !isFloorAt(neighbor.above) in location.cardinals {
}


To me that’s just as good and perhaps even better since it groups the name of the thing with the rules for the thing.

l8r
Sean



> On Jun 24, 2016, at 10:10 AM, Charlie Monroe <charlie at charliemonroe.net> wrote:
> 
> If the `where` keyword were to stay in the language, how would you feel about extending it? One major argument is that it is not as powerful as guard or if. How about something like this was allowed:
> 
> for text in self.texts where let data = text.data(usingEncoding: NSASCIIEncoding) {
> 	/// Do something with data or text
> }
> 
> 
>> On Jun 24, 2016, at 5:06 PM, Sean Heber via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> I’ll share some of mine from a single game project. Note this hasn’t been converted to Swift 3 and this is not all of them - just a varied sampling:
>> 
>> 
>> for location in random.sample(map, density: 0.007) where map.allowsGrassAt(location) && !map.hasGrassAt(location) {
>> }
>> 
>> for location in type.designatedLocations where location.z == Global.cameraZ && !locations.contains(location) {
>> }
>> 
>> for location in region where location.z == mouseLocation.z {
>> }
>> 
>> for location in path where location.z == Global.cameraZ {
>> }
>> 
>> for below in location.below.cardinals where contains(below) && hasSurfaceAt(below) {
>> }
>> 
>> for neighbor in location.cardinals where contains(neighbor) && isRampAt(neighbor) && !isFloorAt(neighbor.above) {
>> }
>> 
>> for neighbor in cardinals where contains(neighbor) && !isSolidAt(neighbor) {
>> }
>> 
>> for console in consoles.reverse() where console.hitTest(vector) {
>> }
>> 
>> 
>> 
>> And these 2 made good use, IMO, of “if-where” but I think that’s no longer in Swift 3? :/
>> 
>> 
>> if let there = debugClickedSurfaceAt, here = mouseSurface where Global.debugPathTest {
>> }
>> 
>> if let here = mouseAt where Global.debugWalkNeighbors {
>> }
>> 
>> 
>> l8r
>> Sean
>> 
>> 
>> 
>> 
>>> On Jun 24, 2016, at 8:24 AM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> Thank you for collecting and sharing these. 
>>> 
>>> I suspect both of us look at these and say "Ah yes, this supports exactly what I've been saying".
>>> 
>>> -- E
>>> 
>>>> On Jun 23, 2016, at 11:59 PM, William Shipley <wjs at mac.com> wrote:
>>>> 
>>>> Here are some of my real-world examples:
>>>> 
>>>>       for modelUUIDAndInterfaceElement in modelUUIDsToInterfaceElements where !usedInterfaceElements.contains(modelUUIDAndInterfaceElement.1) {
>>>>>>>>       }
>>>> 
>>>>       for anchor in wall.anchors where boundsRect.contains(anchor.origin) {
>>>>>>>>       }
>>>> 
>>>>       for otherWall: Wall in self where otherWall != wall && !removedWalls.contains(otherWall) {
>>>>>>>>       }
>>>> 
>>>>       for wall in self as Set<Wall> {
>>>>           for otherWall in self as Set<Wall> where otherWall != wall {
>>>>>>>>           }
>>>>       }
>>>> 
>>>>       for wall in self as Set<Wall> where !checkedWalls.contains(wall) {
>>>>>>>>       } 
>>>> 
>>>> (x2 on that one)
>>>> 
>>>>       for otherPieceOfFurnitureNode in localFurnitureModelUUIDsToInterfaceElements!.values where otherPieceOfFurnitureNode !== pieceOfFurnitureNode {
>>>>>>>>       }
>>>> 
>>>>       for lineSegmentObject in wallRelatedLineSegments where remainingLineSegments.contains(lineSegmentObject) {
>>>>>>>>       }
>>>> 
>>>>       for colinearLineSegmentObject in remainingLineSegments where colinearLineSegmentObject.angle.isEssentially(infiniteLineAngle: lineSegmentObject.angle) {
>>>>>>>>       }
>>>> 
>>>> 
>>>> I think this is basically as many as are found on all of github?
>>>> 
>>>> -W
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 



More information about the swift-evolution mailing list