Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmbr73/12730054fb0ee1035110a8cc15ae1ef0 to your computer and use it in GitHub Desktop.
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 😜
// ==============================================================================================
// 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