Skip to content

Commit

Permalink
fancy swift filter and sorted(using:) (beeminder#498)
Browse files Browse the repository at this point in the history
Using `.filter` for increased legibility. And using `.sorted(using:)` to
as a cleaner way to achieve the same result as the manual closure-based
sorting, since the manual closure-based sort was only checking one
keypath item anyway.
  • Loading branch information
krugerk authored Oct 23, 2024
1 parent 4526394 commit 5be8685
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions BeeKit/GoalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ extension Goal {

/// A hint for the value the user is likely to enter, based on past data points
public var suggestedNextValue: NSNumber? {
let recentData = self.recentData
for dataPoint in recentData.sorted(by: { $0.updatedAt > $1.updatedAt }) {
if dataPoint.isMeta() {
continue
}
return dataPoint.value
}
return nil
let candidateDatapoints = self.recentData
.filter { !$0.isMeta() }
.sorted(using: [SortDescriptor(\.updatedAt, order: .reverse)])

return candidateDatapoints.first?.value
}
}

0 comments on commit 5be8685

Please sign in to comment.