-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add extension helpers to Array of Rankable. - ArtistDigest and VenueDigest are now Rankable, and do not need specialized extensions on Array anymore.
- Loading branch information
Showing
3 changed files
with
39 additions
and
46 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Rankable.swift | ||
// | ||
// | ||
// Created by Greg Bolsinga on 6/8/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol Rankable: LibraryComparable, Hashable, PathRestorable { | ||
var firstSet: FirstSet { get } | ||
func ranking(for sort: RankingSort) -> Ranking | ||
} | ||
|
||
extension Array where Element: Rankable { | ||
func ranked(by sort: RankingSort) -> [Ranking: [Element]] { | ||
self.reduce(into: [Ranking: [Element]]()) { | ||
let ranking = $1.ranking(for: sort) | ||
var arr = ($0[ranking] ?? []) | ||
arr.append($1) | ||
$0[ranking] = arr | ||
} | ||
} | ||
|
||
var firstSeen: [PartialDate: [Element]] { | ||
self.reduce(into: [PartialDate: [(Element, FirstSet)]]()) { | ||
let firstSet = $1.firstSet | ||
var arr = ($0[firstSet.date] ?? []) | ||
arr.append(($1, firstSet)) | ||
$0[firstSet.date] = arr | ||
}.reduce(into: [PartialDate: [Element]]()) { | ||
$0[$1.key] = $1.value.sorted(by: { lhs, rhs in | ||
lhs.1.rank < rhs.1.rank | ||
}).map { $0.0 } | ||
} | ||
} | ||
} |
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