Last active
September 28, 2022 09:17
-
-
Save nmbr73/3a07bbab8c2f8d6dddbca7af5c9ef49a to your computer and use it in GitHub Desktop.
How I tried to tackle the ninth day challenge of #100DaysOfSwiftUI ... having a syntax issue with example C here.
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
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] | |
print("--- A ---") | |
// First approach is with closures as parameters ... but how to do the "print one item per line" | |
// without a for loop?!? We don't know forEach yet, or do we? | |
for n in luckyNumbers | |
.filter({ !$0.isMultiple(of: 2) }) | |
.sorted(by: { $0 <= $1 }) | |
.map( { "\($0) is a lucky number" } ) { | |
print(n) | |
} | |
print("--- B ---") | |
// Then shortened it using trailing closures ... so far so good | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } .sorted { $0 <= $1 } .map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
print("--- C ---") | |
// But what I don't get is: Why is a space as a separator allowed, but a newline isn't ... the | |
// following code does not run - only difference is, that I added line breaks (and there have | |
// been spaces before already): | |
for n in luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } | |
.sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number"} { | |
print(n) | |
} | |
print("--- D ---") | |
// With forEach we can get it running even with the line breaks - and avoiding the loop variable | |
// (as using temporary variables was disallowd according to the description of this echallenge) | |
luckyNumbers | |
.filter { !$0.isMultiple(of: 2) } | |
.sorted { $0 <= $1 } | |
.map { "\($0) is a lucky number" } | |
.forEach { print($0) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment