Last active
September 28, 2022 09:16
-
-
Save nmbr73/12730054fb0ee1035110a8cc15ae1ef0 to your computer and use it in GitHub Desktop.
A basic syntax question I'm having just starting out with the Swift programming language ... I wished @twostraws would have added a video on whitespace for the #100DaysOfSwitUI π
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================================================== | |
// How can it be explained that Swift interprets whitespace and whitespace so differently?!? | |
// ---------------------------------------------------------------------------------------------- | |
// Must be some "a line break makes a chain of commands a sequence and therefore a line | |
// break cannot be used in places where a semicolon would obviously not be allowed". But | |
// I don't see it and some explanation would be much appreciated! | |
// ============================================================================================== | |
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] | |
// β This one does compile ... | |
for n in luckyNumbers .filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } .map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β And this one does compile ... | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } .map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β As well as this one ... | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } .map | |
{ "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β Or this ... | |
for n in luckyNumbers .filter | |
{ !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } .map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β Even this compiles ... | |
for n in luckyNumbers .filter | |
{ !$0.isMultiple(of: 2) } .sorted | |
{ $0 <= $1 } .map | |
{ "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β But neither this ... | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β Nor this .. | |
for n in luckyNumbers .filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// β And regretably this (π my preferd formatting in this case) does not compile either ... | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } | |
.sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
// ---------------------------------------------------------------------------------------------- | |
// Maybe not easy to spot and so if you haven't seen it yet: All of the examples above | |
// only differ in where the line breaks have been placed - nothing else has been changed. | |
// But for the following ones I actually changed the code itself to make it work despite | |
// the line breaks ... but this is not really what the problem described above is about. | |
// ---------------------------------------------------------------------------------------------- | |
// β Sure, this one (π my prefered solution) works - but it's something completely different ... | |
luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } | |
.sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number" } | |
.forEach { print($0) } | |
// β And yes, you get the formatting right avoiding trailing closures - which leads to bad formatting ... | |
for n in luckyNumbers | |
.filter({ !$0.isMultiple(of: 2) }) | |
.sorted(by: { $0 <= $1 }) | |
.map( { "\($0) is a lucky number" } ) { | |
print(n) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment