Last active
November 14, 2016 11:56
-
-
Save rbresjer/c6ddf40781ec019c2936cc38ddd79392 to your computer and use it in GitHub Desktop.
Functional Swift vs non-functional Swift
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
/////////////////////////////////////// | |
// - Non-functional | |
/////////////////////////////////////// | |
var buffer: [Match] = [] | |
var bufferDate: Int = 0 | |
var sections: [FeedSection] = [] | |
for match in matches { | |
if bufferDate != match.createdAt.dateInt { | |
if let firstMatch = buffer.first { | |
sections.append(FeedSection(title: firstMatch.createdAt.prettyString(), matches: buffer)) | |
buffer.removeAll() | |
} | |
bufferDate = match.createdAt.dateInt | |
} | |
buffer.append(match) | |
} | |
if let firstMatch = buffer.first { | |
sections.append(FeedSection(title: firstMatch.createdAt.prettyString(), matches: buffer)) | |
} | |
self.sections = sections | |
/////////////////////////////////////// | |
// - Functional | |
/////////////////////////////////////// | |
self.sections = matches | |
.categorise { | |
$0.createdAt.prettyString() | |
} | |
.map { | |
FeedSection(title: $0.key.uppercased(), matches: $0.value) | |
} | |
.sorted { | |
$0.0.matches.first!.createdAt.dateInt > $0.1.matches.first!.createdAt.dateInt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment