Skip to content

Instantly share code, notes, and snippets.

@rbresjer
Last active November 14, 2016 11:56
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 rbresjer/c6ddf40781ec019c2936cc38ddd79392 to your computer and use it in GitHub Desktop.
Save rbresjer/c6ddf40781ec019c2936cc38ddd79392 to your computer and use it in GitHub Desktop.
Functional Swift vs non-functional Swift
///////////////////////////////////////
// - 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